There are several questions in StackOverflow regarding the position of the legend in Python's matplotlib.pyplot
. For a single graph, the problem can often be solved by tweaking parameters location
and bbox_to_anchor
. In the following example, the legend can be placed above the graph with bbox_to_anchor = (0.5,1.3)
.
industry_capm_df.plot.bar
ax = plt.axes()
ax.legend(bbox_to_anchor=(0.5,1.3))
Yet, for this other graph, the same parameters (0.5,1.3)
result in a legend slightly out of alignment.
industry_raw_df.plot.bar
ax = plt.axes()
ax.legend(bbox_to_anchor=(0.5,1.3))
Since I got several graphs to plot, I would like legend alignment to be automatic, without having to tweak bbox_to_anchor
every time. How could I solve this?