1

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!!

plot

cph_sto
  • 7,189
  • 12
  • 42
  • 78
  • Wouldn't that be achieved by setting the limits, like `ax2.set_ylim(0, sales.Quantity.max() / 0.3)`? – ImportanceOfBeingErnest Nov 07 '19 at 12:38
  • Actually, I was thinking of something similar to scale down (though not exactly what you suggested). I tried your suggestion but the scale on right hand side `Number of items sold` goes from 1-> 14000. I am looking for something like [this](https://stackoverflow.com/questions/13128647/matplotlib-finance-volume-overlay). Check the answer by @ljk07 and the plot he created. – cph_sto Nov 07 '19 at 12:55

1 Answers1

0
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

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Hi, I placed this code between `ax1.plot(..)` and `ax2.bar(..)`, but it gave a strange result. 1) Along right-hand-side, there are two scales. `0->1` is scaled from bottom to top in addition to `volume` `0 -> 4000` in bottom `30%`. 2) The graph in blue is invisible in bottom `30%`. It is as if bars are superimposed on graph in bottom `30 %`, with later not visible anymore. – cph_sto Nov 07 '19 at 15:38