在技​​术图纸中绘制距离箭头 [英] Plotting distance arrows in technical drawing

查看:61
本文介绍了在技​​术图纸中绘制距离箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个图中指出一个距离.我想到的是他们在技术图纸中的处理方式,在其旁边显示了一个带有箭头的双头箭头,作为文本.

I want to indicate a distance in one of my plots. What I have in mind is the way they do it in technical drawings, showing a double headed arrow with the distance as text beside it.

示例:

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")

结果是这样的(其中两条 hlines 并不是真正需要的,它们只是增加了绘图区域...):

This results in something like this (two of the hlines are not really needed, they just increase the drawing area...):

有没有更快的方法来做到这一点,最好是用箭头在确切的位置结束,而不是在它们应该在的位置下方/上方?自动放置文本的额外点.

Is there a quicker way to do this, preferably with arrowheads which end on the exact spot, not below/above where they should be? Extra points for placing the text automatically as well.

我一直在玩 annotate 但由于必须牺牲字符串,所以这个解决方案对我失去了一些吸引力.感谢您指出箭头样式,当我尝试类似的东西时它不起作用.我想没有办法通过一次调用编写一个小函数来完成它...

I had been playing with annotate but since the string would have to be sacrificed this solution had lost some appeal to me. Thanks for pointing out the arrowstyle though, it wasn't working when I attempted something similar. I guess there is no way around writing a little function to do it with one call...

推荐答案

import matplotlib.pyplot as plt

plt.hlines(7, 0, 2, linestyles='dashed')
plt.hlines(11, 0, 2, linestyles='dashed')
plt.hlines(10, 0, 2, linestyles='dashed')
plt.hlines(8, 0, 2, linestyles='dashed')
plt.annotate(
    '', xy=(1, 10), xycoords='data',
    xytext=(1, 8), textcoords='data',
    arrowprops={'arrowstyle': '<->'})
plt.annotate(
    'D = 1', xy=(1, 9), xycoords='data',
    xytext=(5, 0), textcoords='offset points')

# alternatively,
# plt.text(1.01, 9, 'D = 1')

plt.show()

收益

有关 plt.annotate 可用选项的更多信息,请参阅 this页.

For more information on the many options available with plt.annotate, see this page.

如上所示,文本可以用plt.annotateplt.text 放置.使用 plt.annotate 您可以指定点的偏移量(例如 (5, 0)),而使用 plt.text 您可以指定偏移量(例如 (5, 0))数据坐标中的文本位置(例如(1.01,9)).

As shown above, the text can be placed with either plt.annotate or plt.text. With plt.annotate you can specify the offset (e.g. (5, 0)) in points, whereas with plt.text you can specify the text location in data coordinates (e.g. (1.01, 9)).

这篇关于在技​​术图纸中绘制距离箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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