matplotlib 如何在Matplolib中为2个不同的源绘制多个动画

yqkkidmi  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(202)

在测量链中,嵌入各种测量回路中的每个仪器将记录CSV,我想在单独的图中监控实时图,即图1为仪器1,图2为仪器2 ......等。我尝试实现动画,但没有任何东西输出。csv连续生成数据。
我首先在CSV中生成数据,然后我尝试绘制2个动画并行:我得到了图2的动画,但第一个是冻结。任何帮助感谢。

import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import animation

# making figures
def makeFigure():
    df = pd.read_csv('data.csv')
    data = pd.DataFrame(df)
    x = data['current']
    y1 = data['resistance']
    y2 = data['voltage']
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    # # Plot 1 set of data
    dataset =ax.plot(x,y1)
    return fig,ax,dataset

# Frame rendering function
def renderFrame(i, dataset):
    df = pd.read_csv('data.csv')
    data = pd.DataFrame(df)
    x = data['current']
    y1 = data['resistance']
    y2 = data['voltage']
    # Plot data
    plt.cla()
    dataset, =ax.plot(x,y2)
   
    return dataset

# Make the figures
figcomps1=makeFigure()
figcomps2=makeFigure()

# List of Animation objects for tracking
anim = []

# Animate the figures
for figcomps in [figcomps1,figcomps2]:
    fig,ax,dataset = figcomps
    anim.append(animation.FuncAnimation(fig,renderFrame,fargs=[dataset]))
# plt.gcf()
plt.show()
kpbwa7wx

kpbwa7wx1#

这些线可以绘制2个动画并行阅读数据点从一个CSV文件。它做的工作,虽然有时图变得空白。

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

df = pd.read_csv('data.csv')
data = pd.DataFrame(df)
x = data['current']
y1 = data['resistance']
y2 = data['voltage']
 

fig1, ax1 = plt.subplots(figsize=(4, 4))

def animatex(i):
    ax1.clear()
    ax1.plot(x[i:], y1[i:], color='r')
    ax1.autoscale(enable=True, axis='y')

anix = FuncAnimation(fig1, animatex, interval=1000)

fig2, ax2 = plt.subplots(figsize=(4, 4))

def animatev(i):
    ax2.clear()
    ax2.plot(x[i:], y2[i:], color='b')
    ax2.autoscale(enable=True, axis='y')
aniv = FuncAnimation(fig2, animatev, interval=1000)
plt.show()

相关问题