gpt4 book ai didi

python - Pyplot zorder 丢失了动画

转载 作者:行者123 更新时间:2023-11-28 18:41:12 26 4
gpt4 key购买 nike

我正在尝试使用 pyplot 的 funcanimation 为同一图中的不同对象设置动画。
它的工作方式几乎和我预期的一样,除了不同元素的显示顺序不同。所以绘图曲线、文本和图例显示在图像后面几乎看不到的地方。

这是我的(不是这样)最小的工作示例:

#! /usr/bin/python
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import random

def init():
line_simu.set_data([], [])
time_text.set_text('')
imobj.set_data(np.zeros((100, 100)))
imobj.set_zorder(0)
time_text.set_zorder(10)
return line_simu, time_text, imobj

def animate(i):
imobj.set_zorder(0)
time_text.set_zorder(10)
y_simu = np.linspace(0,100, 100)
x_simu = np.linspace(-10, 10, 100)
line_simu.set_data(x_simu, y_simu)

time_text.set_text('time = %.1f' % i )

global data
imobj.set_data( data + np.random.random((100,1)) * 0.5 )

return line_simu, time_text, imobj


def forceAspect(ax,aspect=1):
im = ax.get_images()
extent = im[0].get_extent()
ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)


fig = plt.figure()
ax = plt.axes(xlim=(-15,15), ylim=(-110, 0) , aspect=1)

data = np.random.random((100,100)) - .5
imobj = ax.imshow( data , extent=[-15,15, -110, 0.0], origin='lower', cmap=plt.cm.gray, vmin=-2, vmax=2, alpha=.7, zorder=0, aspect=1)

line_simu, = ax.plot([], [],"r--", lw=2, markersize=4 , label = "Some curve" , zorder= 2 )

time_text = ax.text(-14.9, -108, '', zorder=10)

l = plt.legend(loc='lower right', prop={'size':8} )
l.set_zorder(200)

forceAspect(ax,aspect=1)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=range( 50), interval=3000, blit=True)

plt.show()

没有动画,我可以使用 set_zorder 轻松控制不同元素的顺序,但是当动画更新图像时,这个顺序就会丢失。我尝试在 init 函数中设置 zorder,然后在 animate 函数中再次设置,但没有成功。

我非常感谢在这方面的任何帮助。

最佳答案

回答我自己的问题:似乎 init() 和 animate() 函数返回对象的顺序控制着它们的显示顺序。此外,这些函数应返回图例对象,以便将其包含在动画中。

这是我修改后的代码:

#! /usr/bin/python
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import random

def init():
imobj.set_data(np.zeros((100, 100)))
line_simu.set_data([], [])
time_text.set_text('time = 0.0')

return imobj , line_simu, time_text, l

def animate(i):
global data
imobj.set_data( data + np.random.random((100,1)) * 0.5 )

imobj.set_zorder(0)
y_simu = np.linspace(-100,-10, 100)
x_simu = np.linspace(-10, 10, 100)
line_simu.set_data(x_simu, y_simu)
time_text.set_text('time = %.1f' % i )


return imobj , line_simu, time_text, l


def forceAspect(ax,aspect=1):
im = ax.get_images()
extent = im[0].get_extent()
ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)



fig = plt.figure()
ax = plt.axes(xlim=(-15,15), ylim=(-110, 0) , aspect=1)

data = np.random.random((100,100)) - .5
imobj = ax.imshow( data , extent=[-15,15, -110, 0.0], origin='lower', cmap=plt.cm.gray, vmin=-2, vmax=2, alpha=1.0, zorder=1, aspect=1)

line_simu, = ax.plot([], [],"r--", lw=2, markersize=4 , label = "Some curve" , zorder= 1 )
time_text = ax.text(-14.0, -108, '', zorder=10)

forceAspect(ax,aspect=1)

l = plt.legend(loc='lower right', prop={'size':8} )

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=range( 50), interval=500, blit=True)

plt.show()

关于python - Pyplot zorder 丢失了动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25845465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com