简体   繁体   English

在 matplotlib 动画中更新 x 轴标签

[英]Updating x-axis labels in matplotlib animation

Here is a toy piece of code that illustrates my problem:这是一段说明我的问题的玩具代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], '-o', animated=True)


def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,


def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    ax.set_xlim(np.amin(xdata), np.amax(xdata))
    return ln,


ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
plt.show()

If I set blit=True then the data points are plotted just how I want them.如果我设置blit=True那么数据点就按照我想要的方式绘制。 However, the x-axis labels/ticks remain static.但是,x 轴标签/刻度保持静态。

If I set blit=False then the x-axis labels and ticks update just how I want them.如果我设置blit=False那么 x 轴标签和刻度会按照我想要的方式更新。 However, none of the data points are ever plotted.但是,没有绘制任何数据点。

How can I get both the plotted data (sine curve) and the x-asis data to update"?如何同时更新绘制的数据(正弦曲线)x 轴坐标数据”?

First concerning blitting: Blitting is only applied to the content of the axes.首先关于 blitting:blitting 仅适用于轴的内容。 It will affect the inner part of the axes, but not the outer axes decorators.它会影响轴的内部部分,但不会影响外部轴装饰器。 Hence if using blit=True the axes decorators are not updated.因此,如果使用blit=True轴装饰器不会更新。 Or inversely put, if you want the scale to update, you need to use blit=False .或者反过来说,如果要更新比例,则需要使用blit=False

Now, in the case from the question this leads to the line not being drawn.现在,在问题的情况下,这会导致未绘制线条。 The reason is that the line has its animated attribute set to True .原因是该线的animated属性设置为True However, "animated" artists are not drawn by default.但是,默认情况下不会绘制“动画”艺术家。 This property is actually meant to be used for blitting;这个属性实际上是用于 blitting 的; but if no blitting is performed it will result in the artist neither be drawn nor blitted.但如果不执行 blitting,它将导致艺术家既不会被绘制也不会被 blitted。 It might have been a good idea to call this property blit_include or something similar to avoid confusion from its name.将此属性blit_include或类似的名称可能是一个好主意,以避免与其名称混淆。
Unfortunately, it looks like it's also not well documented.不幸的是,它看起来也没有很好的记录。 You find however a comment in the source code saying但是,您会在源代码中找到一条评论说

# if the artist is animated it does not take normal part in the # draw stack and is not expected to be drawn as part of the normal # draw loop (when not saving) so do not propagate this change

So in total, one can ignore the presence of this argument, unless you use blitting.所以总的来说,人们可以忽略这个论点的存在,除非你使用 blitting。 Even when using blitting, it can be ignored in most cases , because that property is set internally anyways.即使在使用 blitting 时,在大多数情况下也可以忽略它,因为该属性无论如何都是在内部设置的。

To conclude the solution here is to not use animated and to not use blit .总结这里的解决方案是不使用animated和不使用blit

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], '-o')


def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)


def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    ax.set_xlim(np.amin(xdata), np.amax(xdata))


ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM