matplotlib annotate xycoords=('data','axes fraction')=>TypeError:'NoneType'对象不是iterab

2024-10-02 14:24:13 发布

您现在位置: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('++++', 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()

但是注释掉的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。下面的代码演示了这一点:

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

Tags: importfalsecoordinatedataclipmatplotlibison