修复多行更新颜色的颜色matplotlib

发布于 2025-02-07 20:13:03 字数 1055 浏览 4 评论 0原文

我有一个numpy数组:

  • col [0] = time = xaxis_data
  • col [1:32] = 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) 

I have a numpy array with:

  • col[0]=time=xaxis_data
  • col[1:32]= lines for y axis.

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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2025-02-14 20:13:03

我认为您无法在单行绘图语句中绘制不同的颜色,但是如果您将嵌套循环放入嵌套,则可能是可能的:

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) 

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:

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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文