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

@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.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')

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()

上述两种方法产生相同的输出图像:

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值