gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:51:52 24 4
gpt4 key购买 nike

我想在我的一个情节中指出一个距离。我想到的是他们在技术图纸中的做法,显示一个双头箭头,旁边是距离作为文本。

示例:

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

这会导致类似这样的结果(其中两条线并不是真正需要的,它们只是增加了绘图区域...):Distance marker manual style

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

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

最佳答案

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

关于python - 在技​​术图纸中绘制距离箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14612637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com