Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Plotting with twinx

I'm attempting to have a bar plot and line plot on the same figure using Pandas matplotlib API. However, it is not going very well. I am using twinx() which seems to be the generally accepted way of accomplishing this.

Note that this is done in a Jupyter notebook, with the plot being shown inline. Thank you for the assistance!

fig0, ax0 = matplotlib.pyplot.subplots()
ax1 = ax0.twinx()

trend_df_hours.plot(kind='bar', stacked=True, color=color_list, ax=ax0)
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1)
matplotlib.pyplot.show()
matplotlib.pyplot.close()
like image 865
um8ra Avatar asked Oct 28 '25 15:10

um8ra


1 Answers

What seems to be the problem? Your code appears to work fine.

%matplotlib inline
from matplotlib import pyplot as plt

trend_df_hours = pd.Series(np.random.rand(10))
trend_df_qty = pd.Series(np.random.rand(10))

fig0, ax0 = plt.subplots()
ax1 = ax0.twinx()

trend_df_hours.plot(kind='bar', stacked=True, ax=ax0)
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1)
plt.show()
plt.close()

enter image description here

like image 89
Alexander Avatar answered Oct 30 '25 07:10

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!