I would like to plot muiltiple stacked barplots in different axes, using Pandas and matplotlib
The problem with the following minimal example is that it creates a legend for each axis.
import pandas as pd
import matplotlib.pyplot as plt
d1 = {'a': 1, 'b': 10}
d2 = {'a': 5, 'b': 5}
df = pd.DataFrame([d1,d2])
d1 = {'a': 9, 'b': 3}
d2 = {'a': 5, 'b': 4}
df2 = pd.DataFrame([d1,d2])
fig, axes = plt.subplots(2,1,sharex=True)
df.plot.barh(stacked=True,ax=axes[0])
axes[0].set_ylabel('DataFrame 1')
df2.plot.barh(stacked=True,ax=axes[1])
axes[1].set_ylabel('DataFrame 2')
Is there a way to share the legend for all axes? Could the legend be moved outside the plotting area? The legens is probably quite large.


