python – matplotlib annotate xycoords =(‘data’,’axes fraction’)=> TypeError:’NoneType’对象不可迭代

前端之家收集整理的这篇文章主要介绍了python – matplotlib annotate xycoords =(‘data’,’axes fraction’)=> TypeError:’NoneType’对象不可迭代前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

>>> import matplotlib
>>> matplotlib.__version__
'0.99.1.1'

运行以下代码

import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1,.1,.8,.8])
ax.annotate('++++',xy=(.5,.2),xycoords='data')
ax.annotate('aaaa',.4),xycoords='axes fraction')
#ax.annotate('bbbb',.6),xycoords=('data','axes fraction'))
plt.show()

但注释掉的ax.annotate给出了一个错误

TypeError: 'NoneType' object is not iterable

根据当前的文档,它应该是正确的代码

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.

关于发生了什么的任何想法?

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

import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1,.8])
ax.annotate('cccc',xy=(.3,.5),xycoords='data',clip_on=False)
ax.annotate('dddd',-.1),clip_on=False)
ax.annotate('eeee',xy=(.6,xycoords='axes fraction',clip_on=False)
plt.show()
最佳答案
在matplotlib 1.0中添加了一个元组来指示注释的不同x和y变换. Here’s the relevant commit,如果有人有兴趣的话.您需要升级到最新版本才能使用它.

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

来自:http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.annotate

The annotation_clip attribute contols
the visibility of the annotation when
it goes outside the axes area. If
True,the annotation will only be
drawn when the xy is inside the axes.
If False,the annotation will always
be drawn regardless of its position.
The default is None,which behave as
True only if xycoords is”data”.

你需要使用annotation_clip kwarg,而不是:

import matplotlib.pyplot as plt
plt.figure()
ax=plt.axes([.1,annotation_clip=False)
ax.annotate('dddd',annotation_clip=False)
ax.annotate('eeee',annotation_clip=False)
plt.show()

有关更多讨论,请参阅this thread on the matplotlib-users list(并且还注意到annotation_clip kwarg可能在0.99.1上被破坏,因此您可能需要使用那里的解决方法.)

原文链接:https://www.f2er.com/python/439138.html

猜你在找的Python相关文章