加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – matplotlib annotate xycoords =(‘data’,’axes f

发布时间:2020-12-16 22:17:34 所属栏目:Python 来源:网络整理
导读:import matplotlib matplotlib.__version__'0.99.1.1' 运行以下代码: import matplotlib.pyplot as pltplt.figure()ax=plt.axes([.1,.1,.8,.8])ax.annotate('++++',xy=(.5,.2),xycoords='data')ax.annotate('aaaa',.4),xycoords='axes fraction')#ax.annota

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读