在技​​术图纸中绘制距离箭头

时间:2013-01-30 20:01:52

标签: python matplotlib

我想在我的一个情节中指出距离。我想到的是他们在技术图纸中的方式,显示一个双头箭头,旁边有文字。

示例:

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,它们只是增加了绘图区域......):Distance marker manual style

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

修改 我一直在玩annotate,但由于必须牺牲这个字符串,这个解决方案对我失去了一些吸引力。感谢指出箭头风格,当我尝试类似的东西时它没有工作。 我想没有办法用一个电话写一个小函数来做...

3 个答案:

答案 0 :(得分:23)

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

产量

enter image description here

有关plt.annotate提供的众多选项的详细信息,请参阅this page


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

答案 1 :(得分:11)

尝试使用annotate

annotate ('', (0.4, 0.2), (0.4, 0.8), arrowprops={'arrowstyle':'<->'})

Image produced by annotate command

我不确定自动放置文字。

答案 2 :(得分:1)

我精心设计了一个可以很好地完成这个任务的功能:

import numpy as np
import matplotlib.pyplot as plt

def annotate_point_pair(ax, text, xy_start, xy_end, xycoords='data', text_offset=6, arrowprops = None):
    """
    Annotates two points by connecting them with an arrow. 
    The annotation text is placed near the center of the arrow.
    """

    if arrowprops is None:
        arrowprops = dict(arrowstyle= '<->')

    assert isinstance(text,str)

    xy_text = ((xy_start[0] + xy_end[0])/2. , (xy_start[1] + xy_end[1])/2.)
    arrow_vector = xy_end[0]-xy_start[0] + (xy_end[1] - xy_start[1]) * 1j
    arrow_angle = np.angle(arrow_vector)
    text_angle = arrow_angle - 0.5*np.pi

    ax.annotate(
            '', xy=xy_end, xycoords=xycoords,
            xytext=xy_start, textcoords=xycoords,
            arrowprops=arrowprops)

    label = ax.annotate(
        text, 
        xy=xy_text, 
        xycoords=xycoords,
        xytext=(text_offset * np.cos(text_angle), text_offset * np.sin(text_angle)), 
        textcoords='offset points')

    return label