我想在matplotlib图中注释某些长度。例如,点A和B之间的距离。
为此,我想我可以使用annotate并弄清楚如何提供箭头的开始和结束位置。或者,使用arrow并标记该点。
我试着使用后者,但我想不出如何得到一个双头箭头:
from pylab import *
for i in [0, 1]:
for j in [0, 1]:
plot(i, j, 'rx')
axis([-1, 2, -1, 2])
arrow(0.1, 0, 0, 1, length_includes_head=True, head_width=.03) # Draws a 1-headed arrow
show()如何创建双头箭头?更好的是,有没有另一种(更简单的)方法来标记matplotlib图中的尺寸?
发布于 2013-02-15 18:39:01
可以使用arrowstyle属性更改箭头的样式,例如
ax.annotate(..., arrowprops=dict(arrowstyle='<->'))给出了一个双头箭头。
一个完整的示例可以在大约三分之一的页面中找到,其中可能有不同的样式。
至于在地块上标记尺寸的“更好”方法,我想不出有什么好的方法。
编辑:这是一个完整的例子,如果有帮助,你可以使用它
import matplotlib.pyplot as plt
import numpy as np
def annotate_dim(ax,xyfrom,xyto,text=None):
if text is None:
text = str(np.sqrt( (xyfrom[0]-xyto[0])**2 + (xyfrom[1]-xyto[1])**2 ))
ax.annotate("",xyfrom,xyto,arrowprops=dict(arrowstyle='<->'))
ax.text((xyto[0]+xyfrom[0])/2,(xyto[1]+xyfrom[1])/2,text,fontsize=16)
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
annotate_dim(plt.gca(),[0,0],[np.pi,0],'$\pi$')
plt.show()发布于 2020-12-30 06:58:52
下面的图给出了一个很好的尺寸:注释添加了两次,使用不同的箭头('<->‘和'|-|'),之后将文本放在行的中间,并使用bbox覆盖标签下的线。
axs[0].annotate("", xy=(0, ht), xytext=(w, ht), textcoords=axs[0].transData, arrowprops=dict(arrowstyle='<->'))
axs[0].annotate("", xy=(0, ht), xytext=(w, ht), textcoords=axs[0].transData, arrowprops=dict(arrowstyle='|-|'))
bbox=dict(fc="white", ec="none")
axs[0].text(w/2, ht, "L=200 m", ha="center", va="center", bbox=bbox)

https://stackoverflow.com/questions/14892619
复制相似问题