修改matplotlib中注释箭头的宽度

3
我正在使用 annotatematplotlib 中绘制一个箭头。我想让箭头变得更粗。我想要的效果是一支有两个箭头的箭,其中有一个细边线,我可以控制箭头的宽度,即不改变 linewidth。我尝试了 kwargs,例如在答案之后尝试了 width,但导致了错误。我还尝试了不同的 arrowstyleconnectorstyle 变体,但仍然没有成功。我相信这是一个简单的问题!
到目前为止,我的代码如下:
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()

我正在使用Python 2.7和Matplotlib 1.5.1


所以您想要连接两个箭头的实际线变宽,而不是箭头本身? - DavidG
是的,箭头(比例)也可以,但不包括边缘线(使用 edgecolor 设置其宽度为 lw)。非常感谢。 - kungphil
我认为我有同样的问题,但没有比两个简单的相反方向箭头更好的解决方法。这可能是你所说的更清晰的说明:https://stackoverflow.com/questions/64545470/how-to-remove-the-whitespace-between-the-tip-of-the-arrow-of-a-fancyarrowpatch-a/64545471#64545471 - Ciro Santilli OurBigBook.com
1个回答

1
最简单的方法是使用 FancyBboxPatch 并选择双箭头选项。这种方法的一个棘手之处在于箭头不会围绕其顶部旋转,而是围绕定义箭头主体的矩形边缘旋转。我在旋转位置放置了一个红点来演示这一点。
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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接