简体   繁体   English

修复多线更新图 matplotlib 的颜色

[英]Fix color for multiline updating plot matplotlib

I have a numpy array with:我有一个 numpy 数组:

  • col[0]=time=xaxis_data col[0]=时间=xaxis_data
  • col[1:32]= lines for y axis. col[1:32]= y 轴的线。

Every second a new row of data is added to the array.每秒都会向数组中添加一行新数据。

I am plotting the data and updating the plots, however I cannot get the colors of each line to stay fixed.我正在绘制数据并更新绘图,但是我无法让每条线的颜色保持固定。

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) 

I don't think you can plot different colours in a single line plot statement but if you put in a nested for loop it is then possible:我认为您不能在单行绘图语句中绘制不同的颜色,但是如果您放入嵌套的 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM