【问题标题】:Matplotlib Animate a box plotMatplotlib 动画箱线图
【发布时间】:2018-07-12 02:17:15
【问题描述】:

我正在尝试在数据随时间序列变化时为箱形图制作动画。

我正在处理 matplotlib 动画示例,它展示了它如何与 plot 函数一起使用,但这似乎不适用于 boxplot 函数:

下面的代码可以工作,但是将两行更改为箱线图会出现错误

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

fig, ax = plt.subplots()
line, = ax.plot(np.arange(10))  # <-- ax.boxplot(np.arange(10))
ax.set_ylim(0, 20)


def update(data):
    line.set_ydata(data)  # < -- line = ax.boxplot(data)? 
    return line,


def data_gen():
    i = 0
    while True:
        yield np.arange(10) + i
        i += .1

ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
plt.show()

Boxplot 似乎也没有“set_data”函数或“animated=True”参数。

基本上我希望动画与上面的一样,但描绘的是箱线图而不是线图。

【问题讨论】:

  • mpl.pyplot.boxplot() 返回一个 dict,其中包含您需要更新的所有艺术家。您可能必须自己计算所有四分位数,并确定将数据应用于哪些艺术家。箱线图没有一位艺术家需要更新。

标签: python animation matplotlib boxplot


【解决方案1】:

我自己想通了:这个想法可以是清除轴,并在每一帧中绘制一个新的箱线图,如下所示。

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

fig, ax = plt.subplots()
# line, = ax.boxplot(np.arange(10))  <-- not needed it seems
ax.set_ylim(0, 20)


def update(data):
    ax.cla()  # <-- clear the subplot otherwise boxplot shows previous frame
    ax.set_ylim(0, 20)
    ax.boxplot(x=data)  


def data_gen():
    i = 0
    while True:
        yield np.arange(10) + i
        i += .1

ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
plt.show()

【讨论】:

    【解决方案2】:
    # Unset socks proxy
    unset all_proxy    
    unset ALL_PROXY
    
    # Install missing dependencies:
    pip install pysocks
    
    # Reset proxy
    source ~/.bashrc
    

    【讨论】:

    • 我不明白 - 代理与箱线图有什么关系??!
    猜你喜欢
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2010-12-03
    • 2013-05-11
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多