Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: how to show legend elements horizontally?

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 = plt.subplot() for col in (df.columns):     plt.plot(df[col]) plt.legend(loc="lower left") plt.xticks(rotation=90) plt.show() 

Plot:

enter image description here

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)

enter image description here

like image 655
vestland Avatar asked Feb 25 '19 16:02

vestland


People also ask

How do I make Matplotlib legend horizontal?

Set the figure size and adjust the padding between and around the subplots. Using plot() method, plot lines with the labels line1, line2 and line3. Place a legend on the figure using legend() method, with number of labels for ncol value in the argument.

How do I change the legend position in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I make the legend vertical in Matplotlib?

MatPlotLib with PythonCreate a figure and a set of subplots. Plot the vertical line with red color. The line can have both a solid linestyle connecting all the vertices, and a marker at each vertex. Place a legend on the plot with vertical line.


1 Answers

Specify the ncol parameter in legend. In your case something like:

plt.legend(loc="lower left", ncol=len(df.columns)) 

This is the only line I changed in your script.

Working full code:

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 = plt.subplot() for col in (df.columns):     plt.plot(df[col]) plt.legend(loc="lower left", ncol=len(df.columns)) plt.xticks(rotation=90) plt.show() 
like image 191
Francesco Boi Avatar answered Sep 19 '22 11:09

Francesco Boi