我有 Matplotlib 版本 1.5.1,我面临一个有趣的问题。我想在我的绘图中添加一个箭头,该箭头的两端都有头并指定颜色和宽度。然而,研究Matplotlib 关于此事的文档,我意识到我可能无法两者兼得。我可以有一个面向两端的箭头,但该箭头将具有默认颜色和线宽 - 如果我在
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值,我正在循环中绘制)
您应该能够使用 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)
plt.show()