Skip to content
Advertisement

Matplotlib discrete colorbar

I am trying to make a discrete colorbar for a scatterplot in matplotlib

I have my x, y data and for each point an integer tag value which I want to be represented with a unique colour, e.g.

JavaScript

typically tag will be an integer ranging from 0-20, but the exact range may change

so far I have just used the default settings, e.g.

JavaScript

which gives a continuous range of colours. Ideally i would like a set of n discrete colours (n=20 in this example). Even better would be to get a tag value of 0 to produce a gray colour and 1-20 be colourful.

I have found some ‘cookbook’ scripts but they are very complicated and I cannot think they are the right way to solve a seemingly simple problem

Advertisement

Answer

You can create a custom discrete colorbar quite easily by using a BoundaryNorm as normalizer for your scatter. The quirky bit (in my method) is making 0 showup as grey.

For images i often use the cmap.set_bad() and convert my data to a numpy masked array. That would be much easier to make 0 grey, but i couldnt get this to work with the scatter or the custom cmap.

As an alternative you can make your own cmap from scratch, or read-out an existing one and override just some specific entries.

JavaScript

enter image description here

I personally think that with 20 different colors its a bit hard to read the specific value, but thats up to you of course.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement