【问题标题】:Pyplot zorder lost with animationPyplot zorder 因动画而丢失
【发布时间】:2014-11-08 19:51:19
【问题描述】:

我正在尝试使用 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 函数和 animate 函数中设置 zorder,但没有成功。

我非常感谢您在这件事上提供的任何帮助。

【问题讨论】:

  • 我注意到您在此示例中更新的 line_sumu 不存在于轴范围内,因此无论 zorder 是什么都不会显示。但是如果我解决了这个问题,这实际上对我来说很好,没有任何 zorder 问题,使用 1.3.1。你有什么版本的 matplotlib?
  • 谢谢阿让。我用边界外的线编辑了问题,但这只是为了说明。对你有用是个好消息,我确实有一个旧版本:1.1.1。我会尝试更新它,我会通知你的。
  • 最后,我有机会使用 matplotlib 1.3.1 测试脚本。不幸的是,它不能解决任何问题。您只能通过透明看到曲线/图例/时间字符串,图像保持在顶部并降低绘图的可读性。

标签: python animation matplotlib z-order


【解决方案1】:

回答我自己的问题:似乎 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 2015-04-21
    • 2018-12-26
    相关资源
    最近更新 更多