微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python中的matplotlib.animate使用多处理

如何解决python中的matplotlib.animate使用多处理

我正在尝试使用 python 进程为绘图设置动画,如下所示:

from multiprocessing import Process
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

process_enabled = 1;
print("Process enabled: ",process_enabled)

x = []
y = []
fig = plt.figure()
ax = fig.add_subplot(1,1,1)        

def start_animation():
                 
   # Set up plot to call animate() function periodically   
   ani = animation.FuncAnimation(fig,animate,fargs=(x,y),interval=1000)
   print("Called animate function")
   plt.show()   
   
# This function is called periodically from FuncAnimation
def animate(i,xs,ys):
    
   fx=[0.045,0.02,0.0,0.04,0.015,-0.01,0.045,0.035,0.01,0.055,0.025,-0.005,-0.02,-0.05,-0.03] # fx values        
    
   # Add x and y to lists
   xs.append(dt.datetime.Now().strftime('%H:%M:%s.%f'))
   if(i<len(fx)):                     
      ys.append(fx[i])             

   # Draw x and y lists
   ax.clear()
   if(i<len(fx)):   
      ys_stacked = np.stack((np.array(ys),0.1+np.array(ys)),axis=1)
      ax.plot(xs,ys_stacked)
      
   print("Animating")      

   # Format plot
   if(i<len(fx)):
      plt.xticks(rotation=45,ha='right')
      plt.subplots_adjust(bottom=0.30)
      plt.title('Force/Torque Sensor Data')
      plt.ylabel('Fx (N)')    

if(process_enabled):
    
   p_graph = Process(name='Graph',target=start_animation)
   print("Created graph process")

   p_graph.start()
   print("Started graph process")           
   
else:   

   start_animation()

当我禁用该进程时,start_animation() 函数工作正常,绘图显示并且动画开始。但是,当进程被启用时,进程开始,然后代码在 print("Called animate function") 处中断。没有绘图窗口,终端中也没有错误消息)。

我对 python 和 matplotlib 中的多处理都不熟悉。任何方向将不胜感激。

干杯, 托尼

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。