【发布时间】:2023-03-23 19:30:01
【问题描述】:
matplotlib 社区的一个:
假设我有一条直线:
plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2)
我是否可以沿线而不是图例向这条线添加标签?即类似于以下内容(但不是等高线图,只是普通的线图):
【问题讨论】:
标签: python matplotlib plot
matplotlib 社区的一个:
假设我有一条直线:
plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2)
我是否可以沿线而不是图例向这条线添加标签?即类似于以下内容(但不是等高线图,只是普通的线图):
【问题讨论】:
标签: python matplotlib plot
下面是一个简单的例子来展示如何在不考虑外观的情况下完成它。有关注释图的更多信息,请参阅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')
产量:
【讨论】: