在 matplotlib 中重用 patch 对象而不移动它们的位置
我想自动生成一系列被剪裁成补丁的图。如果我尝试重复使用补丁对象,它会在画布上移动位置。
该脚本(基于 Yann 对之前问题的回答)演示了正在发生的情况。
import pylab as plt
import scipy as sp
import matplotlib.patches as patches
sp.random.seed(100)
x = sp.random.random(100)
y = sp.random.random(100)
patch = patches.Circle((.75,.75),radius=.25,fc='none')
def doplot(x,y,patch,count):
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.scatter(x,y)
ax.add_patch(patch)
im.set_clip_path(patch)
plt.savefig(str(count) + '.png')
for count in xrange(4):
doplot(x,y,patch,count)
第一个图如下所示:
但是在第二个“1.png”中,补丁已经移动了..
但是,再次重新绘制不会移动补丁。 “2.png”和“3.png”看起来与“1.png”完全相同。
谁能指出我做错了什么的正确方向?
实际上,我使用的补丁相对复杂,需要一些时间才能生成 - 如果可能的话,我宁愿不必每一帧都重新制作它们。
I want to automatically generate a series of plots which are clipped to patches. If I try and reuse a patch object, it moves position across the canvas.
This script (based on an answer to a previous question by Yann) demonstrates what is happening.
import pylab as plt
import scipy as sp
import matplotlib.patches as patches
sp.random.seed(100)
x = sp.random.random(100)
y = sp.random.random(100)
patch = patches.Circle((.75,.75),radius=.25,fc='none')
def doplot(x,y,patch,count):
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.scatter(x,y)
ax.add_patch(patch)
im.set_clip_path(patch)
plt.savefig(str(count) + '.png')
for count in xrange(4):
doplot(x,y,patch,count)
The first plot looks like this:
But in the second '1.png', the patch has moved..
However replotting again doesn't move the patch. '2.png' and '3.png' look exactly the same as '1.png'.
Could anyone point me in the right direction of what I'm doing wrong??
In reality, the patches I'm using are relatively complex and take some time to generate - I'd prefer to not have to remake them every frame if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过对每个图使用相同的轴,并在每次迭代后调用 ax.cla() 来清除图,可以避免该问题。
The problem can be avoided by using the same axes for each plot, with
ax.cla()
called to clear the plot after each iteration.unutbu 的答案的另一种选择是使用 copy 包,它可以复制对象。调用
add_patch
后很难看出事情发生了什么变化,但事实确实如此。axes
、figure
、extents
、clip_box
、transform
和window_extent
补丁的属性已更改。不幸的是,这些属性中的每一个的表面打印都会产生相同的字符串,因此看起来它们没有改变。但部分或全部这些属性的底层属性(例如extents
是一个Bbox
)可能已更改。复制调用将允许您为您制作的每个人物获得一个独特的补丁,而无需知道它是什么类型的补丁。这仍然没有回答为什么会发生这种情况,但正如我上面所写的,这是问题的替代解决方案:
您也可以使用
fig.savefig(str(count) + '.png')
。这会显式保存图窗fig
,因为plt.savefig
调用会保存当前图窗,而该图窗恰好是您想要的图窗。An alternative to unutbu's answer, is to use the
copy
package, which can copy objects. It is very hard to see how things are changing after one callsadd_patch
, but they are. Theaxes
,figure
,extents
,clip_box
,transform
andwindow_extent
properties of the patch are changed. Unfortantely the superficial printing of each of these properties results in the same string, so it looks like they are not changing. But the underlying attributes of some or all of these properties, egextents
is aBbox
, are probably changed.The copy call will allow you to get a unique patch for each figure you make, without know what kind of patch it is. This still does not answer why this happens, but as I wrote above it's an alternative solution to the problem:
Also you can use
fig.savefig(str(count) + '.png')
. This explicitly saves the figurefig
where as theplt.savefig
call saves the current figure, which happens to be the one you want.