我正在Revenues
上绘制Volume
和dates
,其中Revenues
为graph
,而Volume
为bar
。我只想将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([[...],[...]]))
的事情,但是不知道怎么做!