将标签添加到轮廓 [英] Adding label to contour

查看:45
本文介绍了将标签添加到轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

import numpy as np
import matplotlib.pyplot as plt

plt.contourf(np.random.random((10,10)), label='my_label')
plt.legend()

产生此警告:

warnings.warn(未找到带标签的对象.")

warnings.warn("No labeled objects found.")

有人知道如何标注轮廓吗?
我只想以最简单的方式在绘图窗口内注释自动生成的轮廓.

Does anyone know how to label contour?
I just want to annotate automatically generated contours inside plot window the easiest possible way.

推荐答案

我使用了annotate() 函数来标记我的填充轮廓:

I used annotate() function to label my filled contour:

在上面的代码中,我改用了 plt.legend():

In above code, instead plt.legend() I used:

plt.annotate('my_label',(8,1),backgroundcolor ='w')

因此,与 label 参数相比,仅需要考虑的其他事项是用户需要知道文本将放置的坐标.还是我想.

So only additional consideration compared to label parameter, is that user needs to know the coordinates where the text will be positioned. Or so I think.

更新:如注释中所建议,用户可以为 xycoords 参数选择 axes fraction 类型,并使用[0,1]范围内的相对参考来设置强制性xy 点参数:

Update: As suggested in the comments, user can choose axes fraction type for xycoords parameter and use relative reference in range [0,1] to set mandatory xy point parameter:

plt.annotate('my_label', (.9, .1), xycoords='axes fraction', backgroundcolor='w')

这篇关于将标签添加到轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆