在Matplotlib动画中更新X轴标签 [英] Updating x-axis labels in matplotlib animation

查看:111
本文介绍了在Matplotlib动画中更新X轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一段玩具代码,说明了我的问题:

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()

如果我设置 blit = True 然后按照我想要的方式绘制数据点。但是,x轴标签/刻度保持不变。

If I set blit=True then the data points are plotted just how I want them. However, the x-axis labels/ticks remain static.

如果我设置 blit = False ,则x-轴标签和刻度线更新了我想要的样式。但是,从未绘制过任何数据点。

If I set blit=False then the x-axis labels and ticks update just how I want them. However, none of the data points are ever plotted.

如何获得绘制的数据(正弦曲线) x-asis数据要更新?

How can I get both the plotted data (sine curve) and the x-asis data to update"?

推荐答案

首先涉及到blitting:blitting仅适用于内容会影响轴的内部,但不会影响轴的外部装饰器,因此,如果使用 blit = True ,则不会更新轴装饰器;反之,如果想要缩放比例,需要使用 blit = False

First concerning blitting: Blitting is only applied to the content of the axes. 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. Or inversely put, if you want the scale to update, you need to use blit=False.

现在,在问题是,这条线没有绘制,原因是该线的动画属性设置为 True 。但是,默认情况下不会绘制动画艺术家,此属性实际上是用于blitting;但是,如果不执行blitting,则将导致艺术家既不会被绘制也不会被绘制。该属性 blit_i包括或类似名称,以免混淆其名称。

不幸的是,它似乎也没有充分记录在案。但是,您可以在源代码中找到注释。

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. However, "animated" artists are not drawn by default. This property is actually meant to be used for blitting; but if no blitting is performed it will result in the artist neither be drawn nor blitted. It might have been a good idea to call this property blit_include or something similar to avoid confusion from its name.
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


总的来说,除非您使用blitting,否则可以忽略此参数的存在。即使使用blitting,在大多数情况下也可以将其忽略,因为该属性始终在内部设置。

So in total, one can ignore the presence of this argument, unless you use blitting. Even when using blitting, it can be ignored in most cases, because that property is set internally anyways.

要得出结论,此处的解决方案是不使用动画且不使用 blit

To conclude the solution here is to not use animated and to not use 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()

这篇关于在Matplotlib动画中更新X轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆