Chris
Chris

Reputation: 1353

matplotlib two charts side-by-side with third overlying the second chart

I am trying to use matplotlib (more specifically the plot method from pandas) to plot two charts side-by-side in an ipython notebook with a third chart overlying the second chart and using a secondary y axis. However, I have been unable to get the overlay to work.

Currently this is my code:

import matplotlib.pyplot as plt
%matplotlib inline

fig, axs = plt.subplots(1,2)
fig.set_size_inches(12, 4)

top10.plot(kind='barh', ax=axs[0])
top10_time_trend.T.plot(kind='bar', stacked=True, legend=False, ax=axs[1])
time_trend.plot(kind='line', ax=axs[1], ylim=0, secondary_y=True)

I get the side-by-side structure I am looking for, but only the first (top10) and last (time_trend) plots are visible. My output is below:

enter image description here

When plotted separately the unshown plot (top10_time_trend) looks like this

enter image description here

What I am trying to accomplish is something that looks like this, i.e. the line chart overlaying the stacked bar.

enter image description here

Upvotes: 1

Views: 2634

Answers (1)

Francisco Chaves
Francisco Chaves

Reputation: 33

The best method to do this is by creating a third axis say:

ax3 = ax[1].twinx()

and then

top10_time_trend.T.plot(kind='bar', stacked=True, legend=False, ax=ax3)

Please let me know if this works for you.

Here you can find an example for the usage of twinx() from matplotlib docs http://matplotlib.org/examples/api/two_scales.html

Upvotes: 1

Related Questions