Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change line width of lines in matplotlib pyplot legend [duplicate]

I would like to change the thickness/width of the line samples featured in the pyplot legend.

Line width of line samples within legend are the same as the lines they represent in the plot (so if line y1 has linewidth=7.0, the legend's corresponding y1 label will also have linewidth=7.0).

I would like the legend lines to be thicker than lines featured in the plot.

For example, the following code generates the following image:

import numpy as np import matplotlib.pyplot as plt  # make some data x = np.linspace(0, 2*np.pi)  y1 = np.sin(x) y2 = np.cos(x)  # plot sin(x) and cos(x) fig = plt.figure() ax  = fig.add_subplot(111) ax.plot(x, y1, c='b', label='y1',linewidth=7.0) ax.plot(x, y2, c='r', label='y2')  leg = plt.legend() plt.show() 

example code plot

I want to set the y1 label in the legend to have linewidth=7.0, while the y1 line featured in the plot has a different width (linewidth=1.0).

I was unsuccessful in finding a solution online. The only related issues had answers for changing the linewidth of the legend bounding box through leg.get_frame().set_linewidth(7.0). This does not change linewidth of the lines within the legend.

like image 286
Alnitak Avatar asked Mar 13 '17 08:03

Alnitak


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 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 I increase the thickness of a line in Matplotlib?

To increase the thickness of error line in a Matplotlib bar chart, we can use err_kw=dict() with their properties.

How do I increase the size of a legend in Matplotlib?

Steps. Create line1 and line2 using two lists with different line widths. To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method. To display the figure, use show() method.


1 Answers

@ImportanceOfBeingErnest 's answer is good if you only want to change the linewidth inside the legend box. But I think it is a bit more complex since you have to copy the handles before changing legend linewidth. Besides, it can not change the legend label fontsize. The following two methods can not only change the linewidth but also the legend label text font size in a more concise way.

Method 1

import numpy as np import matplotlib.pyplot as plt  # make some data x = np.linspace(0, 2*np.pi)  y1 = np.sin(x) y2 = np.cos(x)  # plot sin(x) and cos(x) fig = plt.figure() ax  = fig.add_subplot(111) ax.plot(x, y1, c='b', label='y1') ax.plot(x, y2, c='r', label='y2')  leg = plt.legend() # get the individual lines inside legend and set line width for line in leg.get_lines():     line.set_linewidth(4) # get label texts inside legend and set font size for text in leg.get_texts():     text.set_fontsize('x-large')  plt.savefig('leg_example') plt.show() 

Method 2

import numpy as np import matplotlib.pyplot as plt  # make some data x = np.linspace(0, 2*np.pi)  y1 = np.sin(x) y2 = np.cos(x)  # plot sin(x) and cos(x) fig = plt.figure() ax  = fig.add_subplot(111) ax.plot(x, y1, c='b', label='y1') ax.plot(x, y2, c='r', label='y2')  leg = plt.legend() # get the lines and texts inside legend box leg_lines = leg.get_lines() leg_texts = leg.get_texts() # bulk-set the properties of all lines and texts plt.setp(leg_lines, linewidth=4) plt.setp(leg_texts, fontsize='x-large') plt.savefig('leg_example') plt.show() 

The above two methods produce the same output image:

output image

like image 118
jdhao Avatar answered Sep 24 '22 00:09

jdhao