我想这样做:
我有这个python代码:
import numpy as np
import pylab as plt
a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.array([7,8,6,3,2,1,5,8,4,15])
c = plt.plot(a,b,'.')
d = 5
plt.text(2,3, "d = "+d) #This line is the problem because i have not the value of d !
plt.show()
所以实际上我只是想显示d的值,而且我想显示d的值但是相对比较例如在右下角但不是一些坐标。有可能用Python做到这一点吗?
非常感谢你!
答案 0 :(得分:2)
您可以使用transform
参数放置在从0.0
到1.0
的相对坐标中,例如
plt.text(0.5,
0.67,
"d = {}".format(d),
transform=plt.gca().transAxes)
在本地系统中,(0, 0)
是左下角,(1, 1)
是情节的右上角。 Here is a good reference用于在地块上的不同位置放置和旋转文本。
答案 1 :(得分:0)
您可以使用text
在图中放置文字。默认情况下,坐标是数据坐标,但您可以指定要切换的变换,例如到轴坐标。
plt.text(.96,.94,"d={}".format(d), bbox={'facecolor':'w','pad':5},
ha="right", va="top", transform=plt.gca().transAxes )
您可以使用annotate
在图中的某处生成文字。与text
相比的优势在于,您可以(a)使用附加箭头指向对象,(b)您可以根据简单字符串而不是变换来指定坐标系。 / p>
plt.annotate("d={}".format(d), xy=(p, 15), xytext=(.96,.94),
xycoords="data", textcoords="axes fraction",
bbox={'facecolor':'w','pad':5}, ha="right", va="top")
您可以使用偏移框中的AnchoredText
:
from matplotlib.offsetbox import AnchoredText
a = AnchoredText("d={}".format(d), loc=1, pad=0.4, borderpad=0.5)
plt.gca().add_artist(a)