我想在我的一个图中标出一个距离。我想的是他们在技术图纸上的做法,显示了一个双头箭头,旁边的文字是距离。
示例:
from matplotlib.pyplot import *
hlines(7,0,2, linestyles='dashed')
hlines(11,0,2, linestyles='dashed')
hlines(10,0,2, linestyles='dashed')
hlines(8,0,2, linestyles='dashed')
plot((1,1),(8,10), 'k',) # arrow line
plot((1,1),(8,8), 'k', marker='v',) # lower arrowhead
plot((1,1),(10,10), 'k', marker='^',) # upper arrowhead
text(1.1,9,"D=1")
结果是这样的(两个hline不是真的需要,它们只是增加了绘图区域…):
有没有一种更快的方法可以做到这一点,最好是箭头在正确的地方结束,而不是下面/上面他们应该在哪里? 用于自动放置文本的额外点。
编辑:
我一直在玩annotate
,但是由于必须牺牲字符串,这个解决方案对我失去了一些吸引力。不过,谢谢你指出箭头的样式,当我尝试类似的东西时,它不起作用。
我想没有办法用一个调用来编写一个小函数。。。
收益率
有关
plt.annotate
提供的许多选项的详细信息,请参见this page。如上所示,文本可以用
plt.annotate
或plt.text
放置。使用plt.annotate
可以在点中指定偏移量(例如(5, 0)
),而使用plt.text
可以在数据坐标中指定文本位置(例如(1.01, 9)
)。我已经设计了一个函数来很好地实现这一点:
尝试使用
annotate
:但我不确定自动文本放置。
相关问题 更多 >
编程相关推荐