matplotlib中的箭头属性注释

dur*_*hit 4 python annotate matplotlib

我有Matplotlib版本1.5.1,并且遇到了一个有趣的问题。我想在绘图中添加一个箭头,该箭头的两端各有一个头,并指定了颜色和宽度。但是,在研究Matplotlib文档时,我意识到我可能无法同时拥有这两者。我可以使箭头指向两端,但是该箭头将具有默认的颜色和线宽-如果我将其包含arrowstyle在中arrowprops,则可以使用此选项,也可以arrowstyle在arrow属性中省略并设置颜色和宽度,但是我只有默认箭头。有办法同时获得两者吗?

我有以下代码:

plt.annotate('', xy=(p[0][0]-p[0][2], 0), xycoords='data', xytext=(p[0][0], 0), textcoords='data', arrowprops=dict(arrowstyle: '<|-|>',color='k',lw=2.5))
Run Code Online (Sandbox Code Playgroud)

结果是SyntaxError: invalid syntax

(注意:p这只是列表的列表,我从中获取x和y值,我正在循环绘制)

Rob*_*bie 7

您应该能够使用arrowprops设置颜色,宽度和其他属性。

import matplotlib.pyplot as plt

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

ax.annotate('test', xy=(0.9, 0.9),
             xycoords='data',
             xytext=(0, 0),
             textcoords='data',
             arrowprops=dict(arrowstyle= '<|-|>',
                             color='blue',
                             lw=3.5,
                             ls='--')
           )

ax.set_xlim(-0.1,1)
ax.set_ylim(-0.1,1)
fig.show()
Run Code Online (Sandbox Code Playgroud)

给出这个数字:在此处输入图片说明

这有帮助吗?