Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Horizontal Linelength in Legend

Tags:

Lets say i'm plotting a line with matplotlib and add a legend. Within the legend it says ------ Label. I find the default horizontal length of this line a bit too long when plotting small figure sizes for print.

Is there a property to set ------ Label to --- Label?

like image 496
bioslime Avatar asked Dec 07 '12 15:12

bioslime


People also ask

How do I make the legend horizontal in Matplotlib?

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 you change linewidth in legend Matplotlib?

We can change the line width (line thickness) of lines in Python Matplotlib legend by using the set_linewidth() method of the legend object and the setp() method of the artist objects.

How do I wrap a legend in Matplotlib?

import textwrap [...] renames = {c: textwrap. fill(c, 15) for c in df. columns} df. rename(renames, inplace=True) [...]

How do I control the position of a legend 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.


1 Answers

When you create your legend add the handlelength keyword (doc) which sets the length of the '---' (which is called the handle).

like image 122
tacaswell Avatar answered Oct 09 '22 21:10

tacaswell