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上被破坏,因此您可能需要使用那里的解决方法.)

相关文章

我最近重新拾起了计算机视觉,借助Python的opencv还有face_r...
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Poolin...
记得大一学Python的时候,有一个题目是判断一个数是否是复数...
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic ...
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic v...
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic ...