更改matplotlib中注释箭头的宽度

2024-10-05 14:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用annotatematplotlib中绘制一个箭头。我想把箭再肥一点。我想要的效果是一个带有细边线的双头箭头,在这里我可以控制箭头的宽度,即不改变linewidth。我在this答案后尝试了kwargs,例如width,但这导致了一个错误,我也尝试了arrowstyle和{}的不同变体,运气不好。我相信这很简单!在

到目前为止,我的代码是:

import matplotlib.pyplot as plt

plt.figure(figsize=(5, 5))
plt.annotate('', xy=(.2, .2),  xycoords='data',
            xytext=(.8, .8), textcoords='data',
            arrowprops=dict(arrowstyle='<|-|>',
                            facecolor='w',
                            edgecolor='k', lw=1))
plt.show()

我使用的是python2.7和Matplotlib 1.5.1


Tags: 答案data宽度matplotlib绘制plt箭头this
1条回答
网友
1楼 · 发布于 2024-10-05 14:25:27

最简单的方法是使用^{}和darrow(双箭头)选项。这种方法的一个棘手的部分是箭头不会围绕其尖端旋转,而是围绕定义箭头主体的矩形的边缘旋转。我用一个红点在旋转位置演示。在

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl

fig = plt.figure()
ax = fig.add_subplot(111)

#Variables of the arrow
x0 = 20
y0 = 20
width = 20
height = 2
rotation = 45
facecol = 'cyan'
edgecol = 'black'
linewidth=5

# create arrow
arr = patches.FancyBboxPatch((x0,y0),width,height,boxstyle='darrow',
                             lw=linewidth,ec=edgecol,fc=facecol)

#Rotate the arrow. Note that it does not rotate about the tip
t2 = mpl.transforms.Affine2D().rotate_deg_around(x0,y0,rotation) + ax.transData


plt.plot(x0,y0,'ro') # We rotate around this point
arr.set_transform(t2) # Rotate the arrow


ax.add_patch(arr)

plt.xlim(10, 60)
plt.ylim(10, 60)

plt.grid(True)

plt.show()

给予:

enter image description here

相关问题 更多 >