python线条粗细_更改matplotlibpyplot图例中的线条宽度@ImportanceOfBeingErnest的答案很好,如果您只想更改图例框中的线宽。但我认为这有点复杂,因为在更改图例线宽之前必须复制句柄。此外,它不能更改图例标签字体⼤⼩。以下两种⽅法不仅可以更简洁地改变线宽,还可以改变图例标签⽂本的字体⼤⼩。
⽅法1import 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 _lines():
line.set_linewidth(4)
# get label texts inside legend and set font size
for text _texts():
text.set_fontsize('x-large')
plt.savefig('leg_example')
plt.show()
⽅法2import 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')
matplotlib中subplot
ax.plot(x, y2, c='r', label='y2')
leg = plt.legend()
# get the lines and texts inside legend box
leg_lines = _lines()
leg_texts = _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()
上述两种⽅法产⽣相同的输出图像:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。