我有matplotlib版本1.5.1,我面临一个有趣的问题。我想在我的绘图中添加一个箭头,它的每一端都有头,并且指定颜色和宽度然而,通过学习,我意识到我可能不能两者兼得。我可以有一个面向两端的箭头,但是这个箭头将有默认的颜色和线宽-如果我在我的arrowstyle中包含arrowprops这是一个选项,或者我可以省略arrowstyle并在箭头属性中设置颜色和宽度,但是我只有默认箭头。两者都有办法吗?
我有这个密码:

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))

从而导致SyntaxError: invalid syntax
(注意:p只是一个列表列表,我从中获取x和y值,我在循环中绘制)

最佳答案

您应该能够使用箭头道具来设置颜色、宽度和其他属性。

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()

给出这个数字:python - matplotlib中的箭头属性注释-LMLPHP
这有用吗?

08-05 09:46
查看更多