Python Pandas Plotting Two BARH side by side

Viewed 9640

I am trying to make a plot that is similar like this, enter image description here

The left hand side chart (the plot with legend) is derived from df_2, and the right hand side chart is derived from df_1.

However, I cannot make the two plots side-by-side share the y-axis.

Here is my current way to plot:

df_1[target_cols].plot(kind='barh', x='LABEL', stacked=True, legend=False)
df_2[target_cols].plot(kind='barh', x='LABEL', stacked=True).invert_xaxis()
plt.show()

The code will resulted two plots in two different "canvas".

  1. How can I make them side-by-side sharing the y-axis?
  2. How can I remove the label in y-axis for the left hand side chart (chart derived from df_2)?
2 Answers

At first was using

fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0], axes[1]

but then couldn't place the y-ticks on the right side (so they appear centered), for some reason would not work. Now using 'ax' and 'ax2' and ax.yaxis.tick_right() works!

Related