如何在Matplotlib中为两个不同的数据源绘制多个动画。

huangapple go评论99阅读模式
英文:

How to plot multiple animations in Matplolib for 2 different sources

问题

以下是代码的翻译部分:

  1. import pandas as pd
  2. from matplotlib import pyplot as plt
  3. from matplotlib import animation
  4. # 制作图形
  5. def makeFigure():
  6. df = pd.read_csv('data.csv')
  7. data = pd.DataFrame(df)
  8. x = data['current']
  9. y1 = data['resistance']
  10. y2 = data['voltage']
  11. fig = plt.figure()
  12. ax = fig.add_subplot(1, 1, 1)
  13. # 绘制一组数据
  14. dataset = ax.plot(x, y1)
  15. return fig, ax, dataset
  16. # 帧渲染函数
  17. def renderFrame(i, dataset):
  18. df = pd.read_csv('data.csv')
  19. data = pd.DataFrame(df)
  20. x = data['current']
  21. y1 = data['resistance']
  22. y2 = data['voltage']
  23. # 绘制数据
  24. plt.cla()
  25. dataset, = ax.plot(x, y2)
  26. return dataset
  27. # 制作图形
  28. figcomps1 = makeFigure()
  29. figcomps2 = makeFigure()
  30. # 用于跟踪的动画对象列表
  31. anim = []
  32. # 为图形添加动画
  33. for figcomps in [figcomps1, figcomps2]:
  34. fig, ax, dataset = figcomps
  35. anim.append(animation.FuncAnimation(fig, renderFrame, fargs=[dataset]))
  36. # plt.gcf()
  37. 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.

  1. import pandas as pd
  2. from matplotlib import pyplot as plt
  3. from matplotlib import animation
  4. # making figures
  5. def makeFigure():
  6. df = pd.read_csv('data.csv')
  7. data = pd.DataFrame(df)
  8. x = data['current']
  9. y1 = data['resistance']
  10. y2 = data['voltage']
  11. fig=plt.figure()
  12. ax=fig.add_subplot(1,1,1)
  13. # # Plot 1 set of data
  14. dataset =ax.plot(x,y1)
  15. return fig,ax,dataset
  16. # Frame rendering function
  17. def renderFrame(i, dataset):
  18. df = pd.read_csv('data.csv')
  19. data = pd.DataFrame(df)
  20. x = data['current']
  21. y1 = data['resistance']
  22. y2 = data['voltage']
  23. # Plot data
  24. plt.cla()
  25. dataset, =ax.plot(x,y2)
  26. return dataset
  27. # Make the figures
  28. figcomps1=makeFigure()
  29. figcomps2=makeFigure()
  30. # List of Animation objects for tracking
  31. anim = []
  32. # Animate the figures
  33. for figcomps in [figcomps1,figcomps2]:
  34. fig,ax,dataset = figcomps
  35. anim.append(animation.FuncAnimation(fig,renderFrame,fargs=[dataset]))
  36. # plt.gcf()
  37. plt.show()
  38. ```
  39. </details>
  40. # 答案1
  41. **得分**: 1
  42. 以下是您要翻译的代码部分:
  43. ```python
  44. import numpy as np
  45. import matplotlib.pyplot as plt
  46. from matplotlib.animation import FuncAnimation
  47. import pandas as pd
  48. df = pd.read_csv('data.csv')
  49. data = pd.DataFrame(df)
  50. x = data['current']
  51. y1 = data['resistance']
  52. y2 = data['voltage']
  53. fig1, ax1 = plt.subplots(figsize=(4, 4))
  54. def animatex(i):
  55. ax1.clear()
  56. ax1.plot(x[i:], y1[i:], color='r')
  57. ax1.autoscale(enable=True, axis='y')
  58. anix = FuncAnimation(fig1, animatex, interval=1000)
  59. fig2, ax2 = plt.subplots(figsize=(4, 4))
  60. def animatev(i):
  61. ax2.clear()
  62. ax2.plot(x[i:], y2[i:], color='b')
  63. ax2.autoscale(enable=True, axis='y')
  64. aniv = FuncAnimation(fig2, animatev, interval=1000)
  65. plt.show()
  66. ```
  67. <details>
  68. <summary>英文:</summary>
  69. these lines can plot 2 animations in parallel reading datapoints from a CSV file. it does the job although sometimes the figure gets blank.
  70. import numpy as np
  71. import matplotlib.pyplot as plt
  72. from matplotlib.animation import FuncAnimation
  73. import pandas as pd
  74. df = pd.read_csv(&#39;data.csv&#39;)
  75. data = pd.DataFrame(df)
  76. x = data[&#39;current&#39;]
  77. y1 = data[&#39;resistance&#39;]
  78. y2 = data[&#39;voltage&#39;]
  79. fig1, ax1 = plt.subplots(figsize=(4, 4))
  80. def animatex(i):
  81. ax1.clear()
  82. ax1.plot(x[i:], y1[i:], color=&#39;r&#39;)
  83. ax1.autoscale(enable=True, axis=&#39;y&#39;)
  84. anix = FuncAnimation(fig1, animatex, interval=1000)
  85. fig2, ax2 = plt.subplots(figsize=(4, 4))
  86. def animatev(i):
  87. ax2.clear()
  88. ax2.plot(x[i:], y2[i:], color=&#39;b&#39;)
  89. ax2.autoscale(enable=True, axis=&#39;y&#39;)
  90. aniv = FuncAnimation(fig2, animatev, interval=1000)
  91. plt.show()
  92. </details>

huangapple
  • 本文由 发表于 2023年2月19日 09:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定