I am plotting Revenues
and Volume
across dates
, with Revenues
as graph
and Volume
as bar
. All, I want is the bars
should be plotted in the lower 30%
of the plot and not encompassing the entire plot. I think, that can be done with matplotlib.transforms.Bbox
but I don't know how. The following is the code:
Data can be found here
.
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!')
Following is the plot. I want that the bars
should be plotted only in the red
box (bottom 30%
of the height) I have marked, insteading spanning the entire height. May be, I have to do something like ax2.set_position(matplotlib.transforms.Bbox([[...],[...]]))
, but don't know how!!