python线条粗细_更改matplotlib pyplot图例中的线条宽度

  • Post author:
  • Post category:python


@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 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.sh