mustapha_1234
mustapha_1234

Reputation: 49

Visualizing 1D data with color using matplotlib

I have two arrays x and y and I would like to plot the r2 value (r2.shape=(1,N)) of those two arrays at the bottom of a figure with color using matplotlib library in python. like the example below


 image exemple

Upvotes: 0

Views: 1434

Answers (1)

sehan2
sehan2

Reputation: 1835

You could do it this way:

N = 100
r2 = np.random.uniform(0,1,(1,N))
r2 = r2.T

fig, ax = plt.subplots()
ax.plot(np.arange(r2.shape[0]), r2[:,0])

my_cmap = plt.get_cmap("viridis")
rescale = lambda y: (y - np.min(y)) / (np.max(y) - np.min(y))

ax.bar(np.arange(len(r2)), height=0.05, width=1, bottom=0, color=my_cmap(rescale(r2[:,0])))
plt.show()

enter image description here

Upvotes: 3

Related Questions