python中图例legend标签内容,Matplotlib,Python中的完全自定义图例

这篇博客介绍如何在不依赖常规标记如'*'或'o'的情况下,使用Matplotlib库绘制图片并创建完全定制的图例。通过利用proxy artist,可以设置字母或数字作为标记,并为它们指定相应的描述。示例代码展示了如何创建带有特定标签和位置的图例,使得图例能够清晰地指示符号的意义。
摘要由CSDN通过智能技术生成

I am using Matplotlib to basically draw a 'picture', not for plotting data.

In the 'picture' I use plt.annotate to label certain parts of the picture.

I now want to make a completely custom legend to indicate the meaning of the symbols.

Is there a way to define custom handles and labels, where the handles must be alphanumeric letters instead of the normal markers like '*' or 'o'.

Is this possible or must I construct the legend manually using plt.annotation?

解决方案

There are a lot of ways to do it, but it's probably easiest to use a proxy artist in this case. You can use arbitrary text as a marker, so it's fairly easy to have fake Line2D's show labels instead of lines.

As an example (the bulk of this is the relatively "fancy" call to annotate):

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

def main():

labels = ['A', 'B', 'C']

positions = [(2, 5), (1, 1), (4, 8)]

descriptions = ['Happy Cow', 'Sad Horse', 'Drooling Dog']

# Plot the data, similar to what you described...

fig, ax = plt.subplots()

ax.imshow(np.random.random((10, 10)), interpolation='none')

for label, xy in zip(labels, positions):

ax.annotate(label, xy, xytext=(20, 20), size=15,

textcoords='offset points',

bbox={'facecolor':'white'},

arrowprops={'arrowstyle':'->'})

# Create a legend with only labels

proxies = [create_proxy(item) for item in labels]

ax.legend(proxies, descriptions, numpoints=1, markerscale=2)

plt.show()

def create_proxy(label):

line = matplotlib.lines.Line2D([0], [0], linestyle='none', mfc='black',

mec='none', marker=r'$\mathregular{{{}}}$'.format(label))

return line

main()

Mh5sb.png

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、付费专栏及课程。

余额充值