简体   繁体   English

如何通过定性cmap使matplotlib循环?

[英]How to make matplotlib cycle through qualitative cmap?

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.我有一些地理数据有一个“社区”label 这是一个数字,尽管它只是一个分类的东西。 There are about 100 communities, and it seems the labelling sequence is spatial;大约有 100 个社区,标签序列似乎是空间的; ie. 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, ...; '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 ,如果 matplotlib 循环通过 colors,那将不是问题,因此“01”获得第一种颜色,“02”获得第二种颜色,...; and only repeating at '13'.并且只在'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;除了默认似乎是将附近的标签用相同的颜色挤压在一起而不是循环,因此'01','02','03,...,具有相同的颜色; as do '13', '14', '15'... and so on.就像'13','14','15'......等等。

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.这似乎应该是一件很容易改变的事情,但我似乎缺乏对 matplotlib 内部工作原理的正确了解,无法准确地确定要做什么。

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...这里有类似的问题表明我只需要使用set_prop_cycle做一些事情,但我仍然没有得到我想要的......

@Peter Prescott, if you haven't found a solution to your problem yet, I think just creating a custom colormap by adding the X sets of colors you need will do the trick. @Peter Prescott,如果您还没有找到解决问题的方法,我认为只需通过添加您需要的 X 组 colors 来创建自定义颜色图就可以了。

X = math.ceil(N / M), M being the number of colors in your base colormap. X = math.ceil(N / M),M 是基本颜色图中 colors 的数量。

In your example, N = 100, M = 12, X = 9.在您的示例中,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)
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM