Python 更改matplotlib pyplot图例中线条的线宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42758897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change line width of lines in matplotlib pyplot legend
提问by Alnitak
I would like to change the thickness/width of the line samples featured in the pyplot legend.
我想更改 pyplot 图例中的线条样本的粗细/宽度。
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
).
图例中线样本的线宽与它们在图中表示的线相同(因此,如果线y1
具有linewidth=7.0
,则图例的相应y1
标签也将具有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()
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
).
我想y1
将图例中的标签设置为 have linewidth=7.0
,而图中的y1
线条具有不同的宽度 ( 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 withinthe legend.
我在网上找不到解决方案。唯一相关的问题是通过leg.get_frame().set_linewidth(7.0)
. 这不会改变线的线宽内的传奇。
采纳答案by jdhao
@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.
如果您只想更改图例框内的线宽,@ImportanceOfBeingErnest 的回答很好。但我认为它有点复杂,因为您必须在更改图例线宽之前复制句柄。此外,它不能更改图例标签字体大小。以下两种方法不仅可以更简洁地更改线宽,还可以更改图例标签文本字体大小。
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:
以上两种方法产生相同的输出图像: