使用多处理在python中设置matplotlib.animate
我正在尝试使用python进程为绘图设置动画,如下所示:使用多处理在python中设置matplotlib.animate,python,matplotlib,animation,process,Python,Matplotlib,Animation,Process,我正在尝试使用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_enab
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.015,0.045,0.035,0.01,
0.055,0.04,0.02,0.025,0.0,-0.005,-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()
来自多处理导入进程的
将日期时间导入为dt
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
将numpy作为np导入
进程_enabled=1;
打印(“已启用流程:”,已启用流程)
x=[]
y=[]
图=plt.图()
ax=图添加子批次(1,1,1)
def start_动画():
#设置绘图以定期调用animate()函数
ani=animation.FuncAnimation(图,animate,fargs=(x,y),间隔=1000)
打印(“称为动画功能”)
plt.show()
#从FuncAnimation定期调用此函数
定义动画(i、xs、ys):
fx=[0.045,0.02,0.0,0.04,0.015,-0.01,0.015,0.045,0.035,0.01,
0.055,0.04,0.02,0.025,0.0,-0.005,-0.005,-0.02,-0.05,-0.03]#fx值
#将x和y添加到列表中
追加(dt.datetime.now().strftime(“%H:%M:%S.%f”))
如果(i)