Matplotlib未知属性" headwidth"和" head_width"

时间:2014-12-22 08:23:42

标签: python python-2.7 matplotlib

我正在尝试调整Matplotlib中箭头的headwidth

这是一个有效的代码:

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,2*np.pi,500)
y = np.sin(t)

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.plot(t,y)

ax.annotate('', xy=(1, -1), xytext=(2, 0),
arrowprops=dict(arrowstyle='<->', facecolor='black'))

Sine plot with double-headed arrow

如图所示,它绘制了一个漂亮的双头箭头。现在当我想改变headwidth时:

ax.annotate('', xy=(1, -1), xytext=(2, 0),
arrowprops=dict(arrowstyle='<->', facecolor='black',headwidth=10))

ax.annotate('', xy=(1, -1), xytext=(2, 0), 
arrowprops=dict(arrowstyle='<->', facecolor='black',head_width=10))

返回的错误是:

AttributeError: Unknown property headwidth

AttributeError: Unknown property head_width

有什么出路吗?

1 个答案:

答案 0 :(得分:11)

当你在arrowprops dict中指定arrowstyle时,你会得到一个FancyArrowPatch而不是YAArrow的实例,它会使用不同的关键字(但是,你可能知道在尝试使用head_width时) 。文档中不直观的是,当您指定具有默认头设置的箭头样式等时,您可以在箭头样式字符串中修改这些特定设置。这是文档here(强调我的)中的相关行:

  

arrowstyle描述了如何绘制花式箭头。它可以是可用箭头样式名称的字符串,带有可选的逗号分隔属性,也可以是其中一个ArrowStyle实例。

如果您将字典更改为以下字典,则可以使用:

arrowprops=dict(arrowstyle='<->, head_width=10', facecolor='black')

请注意,head_width规范位于样式后面的逗号后面的字符串中。 旁白: 10是一个相当大的数字...... 0.5可能会更好!