python中图例legend标签内容_如何在plot legend中显示文本标签?(例如,删除图例中的标签行)...

您可以通过^{}在图例中设置handletextpad和handlelength,如下所示:import matplotlib.pyplot as plt

import numpy as np

# Plot up a generic set of lines

x = np.arange( 3 )

for i in x:

plt.plot( i*x, x, label='label'+str(i), lw=5 )

# Add a legend

# (with a negative gap between line and text, and set "handle" (line) length to 0)

legend = plt.legend(handletextpad=-2.0, handlelength=0)

有关handletextpad和handlelength的详细信息,请参阅文档(linked here,复制如下):handletextpad : float or None

The pad between the legend handle and text. Measured in font-size

units. Default is None, which will take the value from

rcParams["legend.handletextpad"].

handlelength : float or None

The length of the legend handles. Measured in font-size units. Default

is None, which will take the value from

rcParams["legend.handlelength"].

使用上述代码:

TIw05.png

有了几行额外的标签可以有相同的颜色作为他们的行。只需通过legend.get_texts()使用.set_color()。# Now color the legend labels the same as the lines

color_l = ['blue', 'orange', 'green']

for n, text in enumerate( legend.texts ):

print( n, text)

text.set_color( color_l[n] )

yWuWO.png

只要调用plt.legend()就可以得到:

1Znmq.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,使用matplotlib库绘制图形时,可以通过添加图例legend)来标识每个数据系列的含义。 以下是一些常见的用于设置图例标签内容的方法: 1. 使用label参数:在绘制数据时,可以指定每个数据系列的标签,如: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [1, 3, 2, 4] y2 = [3, 2, 4, 1] plt.plot(x, y1, label='Series 1') plt.plot(x, y2, label='Series 2') plt.legend() plt.show() ``` 在这个例子,我们使用plot()方法绘制两个数据系列,并使用label参数为它们添加标签。然后,使用legend()方法显示图例。 2. 使用handles和labels参数:通过指定handles和labels参数,可以自定义图例标签内容和样式,如: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [1, 3, 2, 4] y2 = [3, 2, 4, 1] line1, = plt.plot(x, y1) line2, = plt.plot(x, y2) plt.legend(handles=[line1, line2], labels=['Series 1', 'Series 2']) plt.show() ``` 在这个例子,我们使用plot()方法绘制两个数据系列,并将它们分别赋值给line1和line2变量。然后,使用legend()方法和handles、labels参数来自定义图例标签内容和样式。 3. 使用字典参数:在legend()方法,还可以使用字典参数来设置图例标签内容和样式,如: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [1, 3, 2, 4] y2 = [3, 2, 4, 1] plt.plot(x, y1) plt.plot(x, y2) plt.legend({'Series 1': y1, 'Series 2': y2}) plt.show() ``` 在这个例子,我们使用plot()方法绘制两个数据系列。然后,使用legend()方法和字典参数来设置图例标签内容和样式。注意,字典的键是标签内容,值是对应的数据系列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值