Matplotlib-覆盖图表,但框大小不同

我正在Revenues上绘制Volumedates,其中Revenuesgraph,而Volumebar。我只想将bars绘制在图的下部30%中,而不要覆盖整个图。我认为,可以使用here完成,但我不知道如何做。以下是代码:

可以找到数据matplotlib.transforms.Bbox

import matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize']=(20,10) # set the figure size
plt.style.use('fivethirtyeight') # using the fivethirtyeight matplotlib theme

sales = pd.read_csv('sales.csv') # Read the data in
sales.Date = pd.to_datetime(sales.Date) #set the date column to datetime
sales.set_index('Date',inplace=True) #set the index to the date column
print(sales)

fig,ax1 = plt.subplots()
ax2 = ax1.twinx()  # set up the 2nd axis
ax1.plot(sales.Sales_Dollars) #plot the Revenue on axis #1

#ax2.set_position(matplotlib.transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
ax2.bar(sales.index,sales.Quantity,width=20,alpha=0.2,color='orange')
ax2.grid(b=False) # turn off grid #2

ax1.set_title('Monthly Sales Revenue vs Number of Items Sold Per Month')
ax1.set_ylabel('Monthly Sales Revenue')
ax2.set_ylabel('Number of Items Sold')

plt.show()
print('Done!')

以下为情节。我希望只在我标记的bars框(高度的底部red)中绘制30%,而不要覆盖整个高度。可能是,我必须做类似ax2.set_position(matplotlib.transforms.Bbox([[...],[...]]))的事情,但是不知道怎么做!

here

fengyun0919 回答:Matplotlib-覆盖图表,但框大小不同

fig,ax1 = plt.subplots()
bb = ax1.get_position()
ax2 = fig.add_axes([bb.x0,bb.y0,bb.width,bb.height*0.3])
ax2.yaxis.tick_right()
ax2.xaxis.set_visible(False)
ax2.spines['top'].set_visible(False)

enter image description here

本文链接:https://www.f2er.com/3145179.html

大家都在问