【问题标题】:Plot data in one column depending on values in another根据另一列中的值绘制一列中的数据
【发布时间】:2016-08-10 23:27:23
【问题描述】:

我想绘制一列中的数据并根据另一列中的相应值更改颜色。我可以通过循环每一行来做到这一点:

 for a in range(0, len(ben_chan)-1):

             if ben_chan[a, int(ben_chan.shape[1])-1] == 0:
                plot1=plt.plot(ben_chan[:,0],ben_chan[:,channel], ".r")
             else:
                plot2=plt.plot(ben_chan[:,0],ben_chan[:,channel], ".b")

有没有更有效的实现方式?

【问题讨论】:

    标签: python-2.7 matplotlib


    【解决方案1】:

    使用 plt.scatter 解决

     plt.scatter(ben_chan[:,0], ben_chan[:,channel], c=int(ben_chan.shape[1])-1], cmap"prism", s=100)
    

    Scatter plot and Color mapping in Python

    此外,如果提取列中满足条件的行并将其设置为新的 numpy 数组,则可以使用更不同的颜色进行绘图:

         ben_stab = ben_chan[ben_chan[:,int(ben_chan.shape[1])-1] > 0] ## Extract rows where elements of ben_chan in column int(ben_chan.shape[1])-1 are > 0 and set as array ben_stab
    
         ben_unstab = ben_chan[ben_chan[:,int(ben_chan.shape[1])-1] == 0] ## Extract rows where elements of ben_chan in column int(ben_chan.shape[1])-1 are = 0 and set as array ben_unstab
    
         plot1=plt.plot(ben_stab[:,0],ben_stab[:,channel], '.b', markersize = 10)
    
         plot2=plt.plot(ben_unstab[:,0],ben_unstab[:,channel], '.r', markersize = 10) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多