英文:
How to plot multiple animations in Matplolib for 2 different sources
问题
以下是代码的翻译部分:
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import animation
# 制作图形
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)
# 绘制一组数据
dataset = ax.plot(x, y1)
return fig, ax, dataset
# 帧渲染函数
def renderFrame(i, dataset):
df = pd.read_csv('data.csv')
data = pd.DataFrame(df)
x = data['current']
y1 = data['resistance']
y2 = data['voltage']
# 绘制数据
plt.cla()
dataset, = ax.plot(x, y2)
return dataset
# 制作图形
figcomps1 = makeFigure()
figcomps2 = makeFigure()
# 用于跟踪的动画对象列表
anim = []
# 为图形添加动画
for figcomps in [figcomps1, figcomps2]:
fig, ax, dataset = figcomps
anim.append(animation.FuncAnimation(fig, renderFrame, fargs=[dataset]))
# plt.gcf()
plt.show()
请注意,代码中的注释部分也已被翻译。如果您需要任何进一步的帮助,请随时提问。
英文:
In a measurement chain, each instrument embedded in various measurement loops will record a CSV and I want to monitor the live plots in separate figures i.e figure 1 for instrument1 , figure 2 for instrument2...etc. I try to implement animations but nothing out. csv is continuously generating data.
I first generate data in a CSV then i try to plot 2 animations in parallel:I get the figure 2 animated but the first is frozen. any help appreciated.
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()
```
</details>
# 答案1
**得分**: 1
以下是您要翻译的代码部分:
```python
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()
```
<details>
<summary>英文:</summary>
these lines can plot 2 animations in parallel reading datapoints from a CSV file. it does the job although sometimes the figure gets blank.
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()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论