Python 在技术制图中绘制距离箭头
我想在我的一个图中指出一个距离。我想的是他们在技术图纸中的做法,在旁边显示一个带距离的双头箭头 示例:Python 在技术制图中绘制距离箭头,python,matplotlib,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',
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
,但是由于必须牺牲字符串,这个解决方案对我失去了一些吸引力。感谢您指出箭头样式,但当我尝试类似的操作时,它不起作用。
我想没有办法编写一个小函数来完成一个调用…尝试使用
annotate
:
annotate ('', (0.4, 0.2), (0.4, 0.8), arrowprops={'arrowstyle':'<->'})
annotation(“”,(0.4,0.2),(0.4,0.8),arrowprops={'arrowstyle':“”})
但是,我不确定是否会自动放置文本。将matplotlib.pyplot作为plt导入
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.hline(7,0,2,linestyles='虚线')
plt.hline(11,0,2,linestyles='虚线')
plt.hline(10,0,2,linestyles='虚线')
plt.hline(8,0,2,linestyles='虚线')
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')
#或者,
#plt.text(1.01,9,'D=1')
plt.show()
屈服
有关plt.annotate
提供的许多选项的更多信息,请参阅
如上所示,文本可以用
plt.annotate
或plt.text
放置。使用plt.annotate
可以指定点的偏移量(例如(5,0)
),而使用plt.text
可以指定数据坐标中的文本位置(例如(1.01,9)
)。我精心设计了一个函数,可以很好地实现这一点:
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
将numpy导入为np
将matplotlib.pyplot作为plt导入
def注释点对(ax、text、xy起点、xy终点、xycoords='data',text\u偏移量=6,arrowprops=无):
"""
通过用箭头连接两点来对其进行注释。
注释文字位于箭头中心附近。
"""
如果arrowprops为无:
arrowprops=dict(arrowstyle='')
断言isinstance(文本,str)
xy_文本=((xy_开始[0]+xy_结束[0])/2.,(xy_开始[1]+xy_结束[1])/2.)
arrow_vector=xy_end[0]-xy_start[0]+(xy_end[1]-xy_start[1])*1j
箭头角度=np角度(箭头向量)
文本角度=箭头角度-0.5*np.pi
ax.annotate(
'',xy=xy_端,xycoords=xycoords,
xytext=xy_开始,textcoords=xycoords,
箭头道具=箭头道具)
label=ax.annotate(
文本,
xy=xy_文本,
xycoords=xycoords,
xytext=(文本偏移*np.cos(文本角度),文本偏移*np.sin(文本角度)),
textcoords='offset points')
退货标签
您也可以将数据坐标与注释一起使用。对,但我认为在这种情况下,使用偏移点
更有用,因为如果图形展开,它将防止标签偏离箭头。您将如何调整箭头的大小?@dbliss:当您指定箭头样式
时,您会得到一个FancyArrowPatch
,它提供了一个固定集的选择不允许自定义箭头的样式,AFAIK。但是,如果删除arrowstyle='
,则会得到一个YAArrow
,它允许您将width
,frac
,headwidth
arrowprops键/值设置为arrowprops
。@ali_m:include{'shrinkA':0,'shrinkB':0}
。这将删除用于收缩箭头的默认两点。