Python 绘图 matplotlib.pyplot 中的箭头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53538909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
arrow in plot matplotlib.pyplot
提问by K. Soerensen
I am unable to get the arrow to display like I want it. Here is an example of what I am trying to do:
我无法让箭头像我想要的那样显示。这是我正在尝试做的一个例子:
import matplotlib.pyplot as plt
import numpy as np
b = np.arange(5)*-1E-4
a = np.arange(5)
fig, ax = plt.subplots()
ax.plot(a,b, linewidth=3, color="k")
plt.arrow(1,-0.00010,0,-0.00005, shape='full', lw=3, length_includes_head=True, head_width=.01)
plt.show()
As far as I understand, this should produce an arrow starting at (1,-0.00010) and ending at (1,-0.00015) But the result is a much longer line, no longer looking like an arrow, and not starting and stopping at the right points.
据我了解,这应该会产生一个从 (1,-0.00010) 开始到 (1,-0.00015) 结束的箭头但结果是一条更长的线,不再看起来像一个箭头,而不是开始和停止正确的点。
回答by DavidG
Because you are using such small scales, some arguments which you have not explicitly passed to plt.arrow
, will use their defaults, which in your case will not give a nice outcome.
由于您使用的是如此小的比例,因此您未明确传递给的某些参数plt.arrow
将使用其默认值,在您的情况下,这不会产生很好的结果。
Looking at the documentation, if no value for width is passed then the default value is 0.001, then the head width will be 0.003 and the head length will be 0.0015. Because the head width is too small using the default values, and the head length is much too big you get the output seen in the question
查看文档,如果没有传递宽度值,则默认值为 0.001,则头部宽度将为 0.003,头部长度将为 0.0015。因为使用默认值的头部宽度太小,而头部长度太大,你会在问题中看到输出
Therefore, you need to pass in the arguments head_width
and head_length
:
因此,您需要传入参数head_width
和head_length
:
plt.arrow(1, -0.00010, 0, -0.00005, length_includes_head=True,
head_width=0.08, head_length=0.00002)
which gives:
这使:
回答by Marcel Flygare
You can draw arrow with annotate.matplotlib docs
您可以使用注释绘制箭头。matplotlib 文档
a = np.linspace(-2,2, 100)
plt.plot(a, a**2)
plt.annotate("here", xy=(0, 0), xytext=(0, 2), arrowprops=dict(arrowstyle="->"))