Python 带有 matplotlib 的线图上的箭头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34017866/
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 on a line plot with matplotlib
提问by cjorssen
I'd like to add an arrow to a line plot with matplotlib
like in the plot below (drawn with pgfplots
).
我想matplotlib
在下面的图中(用 绘制pgfplots
)向线图中添加一个箭头。
How can I do (position and direction of the arrow should be parameters ideally)?
我该怎么办(理想情况下箭头的位置和方向应该是参数)?
Here is some code to experiment.
下面是一些实验代码。
from matplotlib import pyplot
import numpy as np
t = np.linspace(-2, 2, 100)
plt.plot(t, np.sin(t))
plt.show()
Thanks.
谢谢。
采纳答案by thomas
In my experience this works best by using annotate. Thereby you avoid the weird warping you get with ax.arrow
which is somehow hard to control.
根据我的经验,使用annotate效果最好。因此,您可以避免在ax.arrow
某种程度上难以控制的奇怪翘曲。
EDIT:I've wrapped it into a little function.
编辑:我已经把它包装成一个小功能。
from matplotlib import pyplot as plt
import numpy as np
def add_arrow(line, position=None, direction='right', size=15, color=None):
"""
add an arrow to a line.
line: Line2D object
position: x-position of the arrow. If None, mean of xdata is taken
direction: 'left' or 'right'
size: size of the arrow in fontsize points
color: if None, line color is taken.
"""
if color is None:
color = line.get_color()
xdata = line.get_xdata()
ydata = line.get_ydata()
if position is None:
position = xdata.mean()
# find closest index
start_ind = np.argmin(np.absolute(xdata - position))
if direction == 'right':
end_ind = start_ind + 1
else:
end_ind = start_ind - 1
line.axes.annotate('',
xytext=(xdata[start_ind], ydata[start_ind]),
xy=(xdata[end_ind], ydata[end_ind]),
arrowprops=dict(arrowstyle="->", color=color),
size=size
)
t = np.linspace(-2, 2, 100)
y = np.sin(t)
# return the handle of the line
line = plt.plot(t, y)[0]
add_arrow(line)
plt.show()
It's not very intuitive but it works. You can then fiddle with the arrowprops
dictionary until it looks right.
这不是很直观,但它的工作原理。然后你可以摆弄arrowprops
字典,直到它看起来正确。
回答by elzell
Not the nicest solution, but should work:
不是最好的解决方案,但应该有效:
import matplotlib.pyplot as plt
import numpy as np
def makeArrow(ax,pos,function,direction):
delta = 0.0001 if direction >= 0 else -0.0001
ax.arrow(pos,function(pos),pos+delta,function(pos+delta),head_width=0.05,head_length=0.1)
fun = np.sin
t = np.linspace(-2, 2, 100)
ax = plt.axes()
ax.plot(t, fun(t))
makeArrow(ax,0,fun,+1)
plt.show()
回答by adrianus
Just add a plt.arrow()
:
只需添加一个plt.arrow()
:
from matplotlib import pyplot as plt
import numpy as np
# your function
def f(t): return np.sin(t)
t = np.linspace(-2, 2, 100)
plt.plot(t, f(t))
plt.arrow(0, f(0), 0.01, f(0.01)-f(0), shape='full', lw=0, length_includes_head=True, head_width=.05)
plt.show()
EDIT: Changed parameters of arrow to include position & direction of function to draw.
编辑:更改箭头参数以包括要绘制的函数的位置和方向。