0

I want to plot data from a column and change the colour depending on the corresponding value in another column. I can do it by looping through each row as:

 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")

Is there a more efficient way of implementing this?

Afzal
  • 189
  • 3
  • 14

1 Answers1

0

Solved using 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

Also, can be plot with more distinct colours if rows where condition is met in a column are extracted and set as a new numpy array:

     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) 
Community
  • 1
  • 1
Afzal
  • 189
  • 3
  • 14