matplotlib plot 沿绘图线的标签 [英] matplotlib plot Label along plot line

查看:34
本文介绍了matplotlib plot 沿绘图线的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用于matplotlib社区:

One for the matplotlib community:

假设我有一条直线:

plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2)

我可以沿着线而不是图例向这条线添加标签吗?即,类似于以下内容(但不是等高线图,只是普通的线图):

Am I able to add a label to this line along the line rather than in the legend? i.e., something similar to the following (but rather than a contour plot, just an ordinary line plot):

推荐答案

下面是一个示例,只是为了展示如何在不考虑外观的情况下完成.有关注释图的更多信息,请参见此详细演示.

Below is an example simply to show how it can be done without consideration for appearance. For more information about annotating plots please see this detailed demonstration.

import matplotlib.pyplot as plt
x = [37, 45]; y = [-0.67778, -0.67778]

# as an example for where to place the text we can use the mean
xmean = sum(i for i in x) / float(len(x))
ymean = sum(i for i in y) / float(len(y))

plt.plot(x, y, '--k', lw=1.2)
plt.annotate('some text', xy=(xmean,ymean), xycoords='data')
plt.show() # or plt.savefig('filename.png')

产量:

这篇关于matplotlib plot 沿绘图线的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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