Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the line width of the legend frame, in matplotlib

In matplotlib, how do I specify the line width and color of a legend frame?

like image 432
Yuxiang Wang Avatar asked Sep 27 '13 19:09

Yuxiang Wang


People also ask

How do I change the width of a line in Matplotlib?

Import the various modules and libraries you need for the plot: matplot library matplot , numpy , pyplot . Create your data set(s) to be plotted. In the plot() method after declaring the linewidth parameter, you assign it to any number value you want to represent the desired width of your plot.

How do I control legend size in Matplotlib?

The prop keyword is used to change the font size property. It is used in Matplotlib as Using a prop keyword for changing the font size in legend.

How do I control line thickness in Matplotlib?

Matplotlib allows you to adjust the line width of a graph plot using the linewidth attribute. If you want to make the line width of a graph plot thinner, then you can make linewidth less than 1, such as 0.5 or 0.25.

How do you change the width of a line in Python?

You can use the keyword argument linewidth or the shorter lw to change the width of the line.


2 Answers

For the width: legend.get_frame().set_linewidth(w)

For the color: legend.get_frame().set_edgecolor("red")

like image 93
MOREL Benoît Avatar answered Sep 20 '22 09:09

MOREL Benoît


Got the answer:

legend = axes.legend()
frame = legend.get_frame()

and then we can use frame.set_XXX to specify the properties of the frame.

Didn't find it at first because it's not an input argument of the plt.legend() method.

like image 39
Yuxiang Wang Avatar answered Sep 19 '22 09:09

Yuxiang Wang