问题描述
我正在尝试使用 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 (将#修改为@)