gpt4 book ai didi

python - matplotlib 箭头和纵横比

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:12 25 4
gpt4 key购买 nike

如果我运行这个脚本:

import matplotlib.pyplot as plt
import pylab as plab

plt.figure()
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
plt.arrow(1,1,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='r')
plt.arrow(1.25,0.75,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='g')

plab.axes().set_aspect(0.5)

plt.show()

我明白了: enter image description here请注意,箭头的背面与从 (1,2) 延伸到 (2,1) 的黑线齐平。这是有道理的,但我希望箭头的背面在视觉上垂直于尾部而不改变纵横比。我该怎么做?

最佳答案

我(几乎)一直对这个问题感到恼火,并且大多数情况下最终使用 annotate() 来绘制箭头。例如(缺乏大量调整以最终得到与您的情节相同的结果......):

import matplotlib.pyplot as plt
import pylab as plab

plt.figure()
ax=plt.subplot(211)
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
plt.arrow(1,1,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='r')
ax.set_aspect(0.5)

ax=plt.subplot(212)
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
ax.annotate("",
xy=(1.5, 1.5), xycoords='data',
xytext=(1, 1), textcoords='data',
arrowprops=dict(arrowstyle="-|>",
connectionstyle="arc3"),
)
ax.set_aspect(0.5)

plt.show()

enter image description here

关于python - matplotlib 箭头和纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819215/

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