首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修复用于多行更新绘图矩阵库的颜色

修复用于多行更新绘图矩阵库的颜色
EN

Stack Overflow用户
提问于 2022-06-16 09:34:19
回答 1查看 39关注 0票数 0

我有一个numpy数组:

Y轴的

  • col=time=xaxis_data
  • col1:32=线.

每秒钟都会向数组中添加新的一行数据。

我正在绘制数据和更新情节,但我无法获得每一行的颜色保持不变。

代码语言:javascript
复制
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) 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-16 10:36:44

我不认为您可以在单行绘图语句中绘制不同的颜色,但是如果您在嵌套的for循环中添加了一个嵌套的for循环,则有可能:

代码语言:javascript
复制
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) 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72643638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档