I'd like to set the legend to be displayed horizontally. I do not mean the text of the legend like described in the post Matplotlib legend vertical rotation. My actual case includes an arbitrary number of series specified with a widget. But the following example represents the gist of the challenge:
Snippet:
# Imports
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# data
np.random.seed(123)
x = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
y = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
z = pd.Series(np.random.randn(100),index=pd.date_range('1/1/2000', periods=100)).cumsum()
df = pd.concat([x,y,z], axis = 1)
# plot
ax = df.plot()
plt.legend(loc="lower left")
plt.show()
Plot:
The default layout seems to be vertical.
Looking at the details of help(ax.legend)
and the docs , there does not seem to be a straight forward way to change this to horizontal. Or is there?
Edit - Desired Legend: (using MS Paint)
ncols
in this post is a complete solution, and only a part of the solution in the linked post. Usingncols
there isn't even a required part of the solution. So I would argue that this question is not a duplicate at all. Or am I missing something here?