Does the color of the line depend on the data index for the line graph in matplotlib?

So, I have a 2D data array that creates a graph of many time series on the same axes. At the moment, the color of each line simply cycles through and does not mean anything.

I want to somehow match the color of each row with the index of my data, so the low index dataset looks red and then fades to blue with a high index.

To clarify, each individual line should be the same color in everything, and not fade over time. The difference should be between each line.

Thankyou!

+4
source share
4 answers

Often you pass a color code for a graphing function, but you can also pass a number or an array to a color code and get the colors in response.

So, to color each line according to a variable, do something like this:

numlines = 20 for i in np.linspace(0,1, numlines): plt.plot(np.arange(numlines),np.tile([i],numlines), linewidth=4, color=plt.cm.RdYlBu(i)) 

enter image description here

+11
source

plot(x,y,'r') for red lines

plot(x,y,'b') for blue lines


Need more colors for a decent X'mas? See here .


UPDATES

As you requested, there are too many lines for manual setting of colors. So how about this:

 from matplotlib.pyplot import * x = list(range(10)) amount = 20 for i in range(amount): y = [ji for j in x] c = [float(i)/float(amount), 0.0, float(amount-i)/float(amount)] #R,G,B plot(x, y, color=c) show() 

He gives:

enter image description here

+2
source

Here I use rgb colors to get an array of 200 different colors. I don’t have time to sort them by intensity, but take a few printouts of the array and you can understand how to do it. The idea is to sort by index the sums of (sorted) tuples.

 #colorwheel import matplotlib.pyplot as plt from itertools import permutations from random import sample import numpy as np #Get the color-wheel Nlines = 200 color_lvl = 8 rgb = np.array(list(permutations(range(0,256,color_lvl),3)))/255.0 colors = sample(rgb,Nlines) #Plots x = np.linspace(0,2*np.pi) for i in range(Nlines): plt.plot(i*np.cos(x),i*np.sin(x),color=colors[i]) #color from index plt.savefig("SO_colorwheel.png") plt.show() 

Gives enter image description here

+1
source

if someone is still looking for a way to colorize the curve along the path using some color map without using scatter , I think the best way is to split it into segments and call colormap for the color

 import matplotlib.pyplot as plt import numpy as np def plot_colored(x, y, c, cmap=plt.cm.jet, steps=10): c = np.asarray(c) c -= c.min() c /= c.max() it=0 while it<t.size-steps: x_segm = x[it:it+steps+1] y_segm = y[it:it+steps+1] c_segm = cmap( c[it+steps//2] ) plt.plot(x_segm, y_segm, c=c_segm) it += steps # sample track t = np.r_[0:10:1000j] x = t**.25*np.sin(2*np.pi*t) y = t**.25*np.cos(2*np.pi*t) plt.figure() plot_colored(x, y, t) 

(a smaller step makes it smoother but slower) example

+1
source

All Articles