matplotlib箭头和纵横比

时间:2016-06-14 18:08:58

标签: python matplotlib plot

如果我运行此脚本:

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)延伸的黑线齐平。这是有道理的,但我希望箭头的背面与尾部垂直,而不改变纵横比。我该怎么做?

1 个答案:

答案 0 :(得分:3)

我(几乎)曾经对这个问题感到恼火,并且最终使用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