python - Adding colorbar to a 3 variable colour graph -


from post got how create graph, struggling add scale bar. printing item colors gives [ 0.53800422 0.67490159 0.99172189 1.] i'm guessing scaled color?

my_array = range(20) my_array2 = my_array z = np.array(my_array)     x = np.asarray(my_array)     scaled_x = (x - x.min()) / x.ptp() scaled_z = (z - z.min()) / z.ptp() colors = plt.cm.coolwarm(scaled_z) graph = plt.scatter(my_array, my_array2, c = colors, cmap = colors ) cb = plt.colorbar(graph) cb.set_label('mean value') plt.show() 

i think you've gotten mixed on cmap argument supposed be. it's either colormap object (e.g. plt.cm.coolwarm) or name of colormap (e.g. "coolwarm").

you're trying pass in explicit colors. (the [ 0.53800422 0.67490159 0.99172189 1.] red, green, blue, , alpha values colormap yield 1 of specific data points.)

instead of calculating explicit colors, pass in colormap object directly.

for example, given example code above, re-write as:

import numpy np import matplotlib.pyplot plt  my_array = np.arange(20) my_array2 = my_array z = my_array  graph = plt.scatter(my_array, my_array2, c=z, cmap=plt.cm.coolwarm) cb = plt.colorbar(graph) cb.set_label('mean value')  plt.show() 

enter image description here

if you'd like, use different style:

import numpy np import matplotlib.pyplot plt  x = np.arange(20) y, z = x, x  fig, ax = plt.subplots() graph = ax.scatter(x, y, c=z, cmap='coolwarm', s=200) cb = fig.colorbar(graph)  cb.set_label('mean value', rotation=-90, va='bottom') ax.margins(0.1)  plt.show() 

enter image description here


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -