您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:19 python 字数 5191 阅读 498 来源 www.jb51.cc/python

>>> import matplotlib >>> matplotlib.__version__ '0.99.1.1' 运行以下代码:import matplotlib.pyplot as plt plt.figure() ax=plt.axes([.1, .1, .8, .8]) ax.annotate('++++',

概述

>>> 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()

对于第二个问题,这实际上是记录在案的行为. (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 – matplotlib annotate xycoords =(‘data’,’axes fraction’)=> TypeError:’NoneType’对象不可迭代全部内容,希望文章能够帮你解决python – matplotlib annotate xycoords =(‘data’,’axes fraction’)=> TypeError:’NoneType’对象不可迭代所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶