首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用matplotlib用不同的数据单元动画两个图形

使用matplotlib用不同的数据单元动画两个图形
EN

Stack Overflow用户
提问于 2022-01-24 09:15:07
回答 1查看 73关注 0票数 0

试图绘制两个不同的动画,即在不同的窗口作为不同的数字。为我运行这段代码正确地创建了两个窗口,但同时在第二个图形上动画数据。关闭图1只导致图2的预期数据被动画化,从图1的数据中删除重叠,关闭图2只导致图1的预期数据被动画化,从图2的数据中删除重叠。

最低限度代码如下:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

dx, dv, N, Nb, decp = 2, 1.5, 100, 12, int(1)
Pd = np.zeros([N + 1, 2 * Nb])
Vd = np.zeros([N + 1, 2 * Nb])

Pd[:, 1] = 4
Vd[:, 3] = 2

t = np.zeros(N + 1)
t[0] = 0
for i in range(0, N):
    t[i + 1] = (i + 1) * 0.1

Px = []
for i in range(0, (2 * Nb)):
       
    PX = dx * (-Nb + i) / 4
    Px.append(PX)


lblx = []
for i in range(0, int((Nb / 2) + 1)):
    if i == (Nb / 4):
        LBL = r"$\mu_x$"
        lblx.append(LBL)
    else:
        LBL = r"${0}\sigma_x$".format(-(Nb / 4) + i)
        lblx.append(LBL)


Pv = []
for i in range(0, (2 * Nb)):
       
    PV = dv * (-Nb + i) / 4
    Pv.append(PV)


lblv = []
for i in range(0, int((Nb / 2) + 1)):
    if i == (Nb / 4):
        LBL = r"$\mu_v$"
        lblv.append(LBL)
    else:
        LBL = r"${0}\sigma_v$".format(-(Nb / 4) + i)
        lblv.append(LBL)

fig1 = plt.figure(figsize=(8,6))
def animatex(i):
       
    fig1.clear()
    plt.bar(Px, Pd[i, :], width = dx / 4, align = 'edge', color = 'b', \
        label = 't = {} seconds'.format(round(t[i], decp)))
    s_ticks = np.arange(-3 * dx, (3 + 1) * dx, dx)
    plt.xticks(s_ticks, lblx)
    plt.ylim(0, np.max(Pd))
    plt.xlim(-3 * dx, 3 * dx)
    plt.legend()
    plt.draw()

anix = FuncAnimation(fig1, animatex, repeat = True, interval = 200, frames = N + 1)


fig2 = plt.figure(figsize=(8,6))
def animatev(i):
       
    fig2.clear()
    plt.bar(Pv, Vd[i, :], width = dv / 4, align = 'edge', color = 'b', \
        label = 't = {} seconds'.format(round(t[i], decp)))
    s_ticks = np.arange(-3 * dv, (3 + 1) * dv, dv)
    plt.xticks(s_ticks, lblv)
    plt.ylim(0, np.max(Vd))
    plt.xlim(-3 * dv, 3 * dv)
    plt.legend()
    plt.draw()

aniv = FuncAnimation(fig2, animatev, repeat = True, interval = 200, frames = N + 1)

plt.show()

很可能很清楚,它们是两个条形图,具有不同的垂直和水平尺寸。对于这些问题,我已经看到了一些解决方案,在这些问题中,数据通过共享变量共享一个轴,但在这里它们不是(可以看到的)。对于这个最小的代码,解决方案涉及两个条,一个在Pd中,另一个在Vd中,在它们各自的预期数字上,而不是在第二个数字上。

请告诉我这里的信息是否有任何问题,即没有满足最低限度的代码要求、更多的信息等等,我将进行更新。

忽略任何任性的写作风格,都是不相关的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-24 10:14:53

简化代码:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

dx, dv, N, Nb, decp = 2, 1.5, 10, 12, int(1)

Px = np.arange(Nb)
Pd = np.random.randn(N, Nb)
Vd = np.random.randn(N, Nb)

fig1, ax1 = plt.subplots(figsize=(8, 6))


def animatex(i):
    ax1.clear()
    ax1.bar(Px, Pd[i, :], width=dx / 4, align='edge', color='b')
anix = FuncAnimation(fig1, animatex, repeat=True, interval=200, frames=N)

fig2, ax2 = plt.subplots(figsize=(8, 6))


def animatev(i):
    ax2.clear()
    ax2.bar(Px, Vd[i, :], width = dv / 4, align='edge', color='b')
aniv = FuncAnimation(fig2, animatev, repeat=True, interval=200, frames=N)

plt.show()

对我来说很好。你可以把美感/数据细节加回.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70831431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档