【发布时间】:2021-04-11 09:40:21
【问题描述】:
首先,这是我在这个论坛上的第一篇文章。通常,我可以通过查看此论坛中的帖子来解决我的问题,但在这种情况下,我找不到信息。提前感谢您的支持。
我正在尝试绘制来自一个 PCB 板的多个 IC 的数据(电压和温度)。目前我只是使用随机生成的数字。我在两个不同的图形(子图)中实时显示结果。通过硬写每个 IC 的数据,我已经成功地为固定数量的 IC 做到了这一点,例如line17.set_ydata([np.nan] * len(x))
问题在于 PCB 可以有不同数量的 IC 进行检查。例如,如果 PCB 有 5 个设备,我将需要 10 个 lineXX.set_ydata(..),但另一方面,如果 PCB 有 10 个 IC,那么在这种情况下,我将需要 20 个 lineXX.set_ydata。未来将作为输入参数的IC数量。
我想要的是使用 for 循环替换所有 lineXX.set_ydata(..) 和 linexx, = (用于绘图)。
这是我的代码:
from collections import deque
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.ticker import FuncFormatter
def init():
# I want to replace this code with a for cycle
line0.set_ydata ( [np.nan] * len(x) )
line1.set_ydata ( [np.nan] * len(x) )
line2.set_ydata ( [np.nan] * len(x) )
line3.set_ydata ( [np.nan] * len(x) )
line4.set_ydata ( [np.nan] * len(x) )
line5.set_ydata ( [np.nan] * len(x) )
line6.set_ydata ( [np.nan] * len(x) )
line7.set_ydata ( [np.nan] * len(x) )
line8.set_ydata ( [np.nan] * len(x) )
line9.set_ydata ( [np.nan] * len(x) )
line10.set_ydata( [np.nan] * len(x) )
line11.set_ydata( [np.nan] * len(x) )
line12.set_ydata( [np.nan] * len(x) )
line13.set_ydata( [np.nan] * len(x) )
line14.set_ydata( [np.nan] * len(x) )
line15.set_ydata( [np.nan] * len(x) )
line16.set_ydata( [np.nan] * len(x) )
line17.set_ydata( [np.nan] * len(x) )
print('test')
return
def animate(i):
# Add next value
for ic in range(0, total_ics):
data_temp[ic].append(np.random.randint(0, max_rand))
data_volt[ic].append(np.random.randint(0, max_rand))
# I want to replace this code with a for cycle
line0.set_ydata(data_temp[0])
line1.set_ydata(data_temp[1])
line2.set_ydata(data_temp[2])
line3.set_ydata(data_temp[3])
line4.set_ydata(data_temp[4])
line5.set_ydata(data_temp[5])
line6.set_ydata(data_temp[6])
line7.set_ydata(data_temp[7])
line8.set_ydata(data_temp[8])
# I want to replace this code with a for cycle
line9.set_ydata(data_volt[0])
line10.set_ydata(data_volt[1])
line11.set_ydata(data_volt[2])
line12.set_ydata(data_volt[3])
line13.set_ydata(data_volt[4])
line14.set_ydata(data_volt[5])
line15.set_ydata(data_volt[6])
line16.set_ydata(data_volt[7])
line17.set_ydata(data_volt[8])
#plt.savefig('e:\\python temp\\fig_{:02}'.format(i))
print(i)
return
colors = ['','#1F77B4' ,'#FF7F0E' , '#2CA02C', '#D62728', '#9467BD' , '#8C564B' , '#E377C2' , '#7F7F7F' , '#BCBD22' , '#17BECF']
max_x = 601
max_rand = 100
total_ics = 9
data_temp = []
data_volt = []
for ic in range(0, total_ics ):
data_temp.append(deque(np.zeros(max_x), maxlen=max_x))
data_volt.append(deque(np.zeros(max_x), maxlen=max_x))
x = np.arange(0, max_x)
fig, (ax_temp, ax_volt) = plt.subplots(2, 1 , figsize=(15,8), num='Temperature and Voltage Plotting', constrained_layout=True)
ax_temp.set_ylim(0, max_rand)
ax_temp.set_xlim(0, max_x-1)
ax_volt.set_ylim(0, max_rand)
ax_volt.set_xlim(0, max_x-1)
# I want to replace this code with a for cycle
line0, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[1])
line1, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[2])
line2, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[3])
line3, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[4])
line4, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[5])
line5, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[6])
line6, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[7])
line7, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[8])
line8, = ax_temp.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[9])
line9, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[1])
line10, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[2])
line11, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[3])
line12, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[4])
line13, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[5])
line14, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[6])
line15, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[7])
line16, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[8])
line17, = ax_volt.plot(x, np.random.randint(0, max_rand, max_x), lw=0.2, color=colors[9])
ax_temp.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{:.0f}s'.format(max_x - x - 1 )))
ax_volt.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{:.0f}s'.format(max_x - x - 1 )))
plt.xlabel('Seconds ago')
ani = animation.FuncAnimation(
fig, animate, init_func=init, interval=1000, blit=False, save_count=10)
plt.show()
干杯
【问题讨论】:
标签: python python-3.x numpy matplotlib animation