Scatter Color Chart - Matplotlib

I try to show the color bar of my scatter, but I keep getting an error:

TypeError: you must first set_parameter for the displayed

This is what I do to build:

# Just plotting the values of data that are nonzero x_data = numpy.nonzero(data)[0] # x coordinates y_data = numpy.nonzero(data)[1] # y coordinates # Mapping the values to RGBA colors data = plt.cm.jet(data[x_data, y_data]) pts = plt.scatter(x_data, y_data, marker='s', color=data) plt.colorbar(pts) 

If I comment on the line plt.colorbar(pts) , I understood the plot correctly, but I would also like to build a color panel.

Thanks in advance.

+7
python matplotlib plot colorbar
source share
1 answer

You are passing certain rgb values, so matplotlib cannot create color code because it does not know how it relates to your source data.

Instead of displaying the values ​​in RGB colors, let scatter handle this for you.

Instead:

 # Mapping the values to RGBA colors data = plt.cm.jet(data[x_data, y_data]) pts = plt.scatter(x_data, y_data, marker='s', color=data) 

do:

 pts = plt.scatter(x_data, y_data, marker='s', c=data[x_data, y_data]) 

(Just go to c , from which you originally went to plt.cm.jet .)

Then you can normally create the color code. A specific error tells you that the colors were set manually and not set via set_array (which handles the mapping of data values ​​to RGB).

+7
source share

All Articles