我有一个numpy数组:
Y轴的
每秒钟都会向数组中添加新的一行数据。
我正在绘制数据和更新情节,但我无法获得每一行的颜色保持不变。
import numpy as np
import time
import matplotlib.pyplot as plt
#add time column
start_measurment = time.time()
#storing the updated data
to_plot = np.zeros((1, 33))
#maybe using this? my_colors = plt.rcParams['axes.prop_cycle'][:32]()
fig,ax = plt.subplots(1,1)
ax.set_xlabel('time(s)')
ax.set_ylabel('sim. Data')
for i in range (20): #updating plot 20 times
#simulate the data for Stack example
Simulated_data = (np.arange(32)*i).reshape((1, 32))
#insert the time as col[0]
Simulated_data = np.insert(Simulated_data, 0, [time.time()-start_measurment], axis=1) #insert time
#append new data to a numpy array
to_plot = np.append(to_plot,Simulated_data , axis=0)
#Plot Data
ax.plot(to_plot[:,0], to_plot[:,1:]) #Add here how to fix colours
fig.canvas.draw()
time.sleep(1) 发布于 2022-06-16 10:36:44
我不认为您可以在单行绘图语句中绘制不同的颜色,但是如果您在嵌套的for循环中添加了一个嵌套的for循环,则有可能:
import numpy as np
import time
import matplotlib.pyplot as plt
#add time column
start_measurment = time.time()
#storing the updated data
to_plot = np.zeros((1, 33))
#maybe using this? my_colors = plt.rcParams['axes.prop_cycle'][:32]()
fig,ax = plt.subplots(1,1)
ax.set_xlabel('time(s)')
ax.set_ylabel('sim. Data')
for i in range (100): #updating plot 20 times
#simulate the data for Stack example
Simulated_data = (np.arange(32)*i).reshape((1, 32))
#insert the time as col[0]
Simulated_data = np.insert(Simulated_data, 0, [time.time()-start_measurment], axis=1) #insert time
#append new data to a numpy array
to_plot = np.append(to_plot,Simulated_data , axis=0)
#Plot Data
for j in range(1,len(to_plot[0])-1):
ax.plot(to_plot[:,0], to_plot[:,j:j+1],c = f"C{j}") #Add here how to fix colours
fig.canvas.draw()
time.sleep(1) https://stackoverflow.com/questions/72643638
复制相似问题