3

I'm afraid is isn't a reproducible example but I'll try to clearly explain my problem.

I have some geographic data that has a 'community' label that is a number, although it's just a categorical thing. There are about 100 communities, and it seems the labelling sequence is spatial; ie. consecutive labels are near to each other. I want to visualize the data using one of the qualitative colormaps, eg. 'Paired' (cf. https://matplotlib.org/stable/tutorials/colors/colormaps.html), which only has 12 colors, but as there are never (or at least rarely) more than twelve communities in a small area, that would not be a problem if matplotlib cycled through the colors, so that '01' gets the first color, '02' the second, ...; and only repeating at '13'.

Except instead the default seems to be to squeeze nearby labels together with the same color rather than cycling, so that '01','02','03, ..., have the same color; as do '13', '14', '15'... and so on.

This seems like it should be a simple thing to change, but I seem to lack the proper knowledge of matplotlib's inner workings to work out exactly what to do.

There's similar question here that suggests I just need to do something with set_prop_cycle, but I'm still not getting what I want...

1
  • We are going to need some examples of what you tried already to know what it is you want to do. You mention "except instead the default seems to...". Where is the example code and plot of this? Commented Nov 30, 2021 at 11:09

1 Answer 1

0

 I think just creating a custom colormap by adding the X sets of colors you need will do the trick.

X = math.ceil(N / M), M being the number of colors in your base colormap.

In your example, N = 100, M = 12, X = 9.

Simple application:

cmap = plt.cm.tab20                           # get a specific colormap
cmaplist = cmap.colors                        # extract all colors
cmaplistext = cmaplist + cmaplist             # repeat X times, here X = 2

customMap = matplotlib.colors.LinearSegmentedColormap.from_list('Custom cmap', cmaplistext, N)
plt.imshow(data, customMap)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.