Python matplotlib注释xycoords=(';数据';,';轴分数';)=>;类型错误:';非类型';对象是不可编辑的

Python matplotlib注释xycoords=(';数据';,';轴分数';)=>;类型错误:';非类型';对象是不可编辑的,python,matplotlib,Python,Matplotlib,运行以下代码: >>> import matplotlib >>> matplotlib.__version__ '0.99.1.1' 但是注释掉的ax.annotate给出了一个错误: import matplotlib.pyplot as plt plt.figure() ax=plt.axes([.1, .1, .8, .8]) ax.annotate('++++', xy=(.5,.2), xycoords='data') ax.annotate('

运行以下代码:

>>> import matplotlib
>>> matplotlib.__version__
'0.99.1.1'
但是注释掉的ax.annotate给出了一个错误:

import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1, .1, .8, .8])
ax.annotate('++++', xy=(.5,.2), xycoords='data')
ax.annotate('aaaa', xy=(.5,.4), xycoords='axes fraction')
#ax.annotate('bbbb', xy=(.5,.6), xycoords=('data', 'axes fraction'))
plt.show()
根据当前文档,它应该是正确的代码:

TypeError: 'NoneType' object is not iterable
有什么想法吗

编辑: 自从第一次发帖以来,我一直在寻找解决方法,并发现了另一个问题。当xycoords='data'时,如果注释落在轴之外,即使使用clip_on=False,注释仍会被剪裁。下面是演示这一点的代码:

From: http://matplotlib.sourceforge.net/users/annotations_guide.html

4. A tuple of two coordinate specification. 
   The first item is for x-coordinate and 
   the second is for y-coordinate. 
   For example,

        annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))

    0.5 is in data coordinate, and 1 is in normalized axes coordinate.

matplotlib
1.0
中添加了一个元组,用于指示注释的不同x和y变换,如果有人感兴趣的话。您需要升级到最新版本才能使用它

对于第二个问题,这实际上是记录在案的行为。(
clip_on
是一种通用的matplotlib文本艺术家kwarg。显然,注释艺术家的行为方式不同。)

发件人:

注释剪辑属性控件 注释的可见性 它位于轴区域之外。如果 如果为True,则注释将仅为 当xy位于轴内时绘制。 如果为False,则注释将始终 无论其位置如何,都可以绘制。 默认值为“无”,其行为类似于 仅当xycoords为“数据”时为True

您需要使用
annotation\u clip
kwarg,而不是:

import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1, .1, .8, .8])
ax.annotate('cccc', xy=(.3, .5), xycoords='data', clip_on=False)
ax.annotate('dddd', xy=(.3,-.1), xycoords='data', clip_on=False)
ax.annotate('eeee', xy=(.6,-.1), xycoords='axes fraction', clip_on=False)
plt.show()

有关更多讨论,请参见(同时请注意,
0.99.1
上的
annotation\u clip
kwarg可能已损坏,因此您可能需要使用此处的解决方法。)

您使用的matplotlib版本是什么?你的两个例子对我来说都很好…谢谢Joe,matplotlib.\uuuuuu版本\uuuuuu'0.99.1.1'。。。。您使用的是什么版本?@mike-
1.0.1
我认为
xcoords
kwarg的“两坐标规范元组”部分是在matplotlib 1.0中添加的。参见2010年2月25日的新增内容,后一个问题可能是某个地方修复的错误。(事实上,我敢打赌这是一个只存在于
0.99.1
中的bug。我不认为它会被长期忽视。)@mike-事实上,后一个问题即使在较新版本的maplotlib中也会持续存在。然而,这实际上是预期的行为。看一下
注释\u剪辑
kwarg to annotation:@Joe-好的,我现在安装了1.0.1(没有卸载或删除0.99.1.1)。。。我会汇报结果的
import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1, .1, .8, .8])
ax.annotate('cccc', xy=(.3, .5), xycoords='data', annotation_clip=False)
ax.annotate('dddd', xy=(.3,-.1), xycoords='data', annotation_clip=False)
ax.annotate('eeee', xy=(.6,-.1), xycoords='axes fraction', annotation_clip=False)
plt.show()