Horizontal layout of patches in legend (matplotlib)

I create legend in my chart this way:

legend_handles.append(matplotlib.patches.Patch(color=color1, label='group1'))
legend_handles.append(matplotlib.patches.Patch(color=color2, label='group2'))
ax.legend(loc='upper center', handles=legend_handles, fontsize='small')

This results in the legend items stacked vertically (top-bottom), while I would like to put them horizontally left to right.

How can I do that?

(matplotlib v1.4.3)

Answer

There is an argument determining the number of columns ncol=.

ax.legend(loc='upper center', handles=legend_handles, fontsize='small', ncol=2)

This should do the trick. Got it from this thread.

Attribution
Source : Link , Question Author : LetMeSOThat4U , Answer Author : Community

Leave a Comment