简体   繁体   English

matplotlib颜色图提供重复的值

[英]matplotlib colormap giving duplicated values

Recently I was using this method to basically select 6 equally values along this colormap. 最近,我使用这种方法基本上沿着该色图选择了6个相等的值。

import matplotlib.pyplot as plt
import numpy as np
ints = np.linspace(0,255,6)
ints = [int(x) for x in ints]
newcm = plt.cm.Accent(ints)

Normally this would return the colormap values no problem. 通常,这将返回颜色图值没有问题。 Now when I run this, the output I get for newcm is: 现在,当我运行此命令时,对于newcm我得到的输出是:

Out[25]: 
array([[ 0.49803922,  0.78823529,  0.49803922,  1.        ],
       [ 0.4       ,  0.4       ,  0.4       ,  1.        ],
       [ 0.4       ,  0.4       ,  0.4       ,  1.        ],
       [ 0.4       ,  0.4       ,  0.4       ,  1.        ],
       [ 0.4       ,  0.4       ,  0.4       ,  1.        ],
       [ 0.4       ,  0.4       ,  0.4       ,  1.        ]])

So now things are not plotting right. 因此,现在情况并非正确。 I have also tried bytes=True but the behaviour is the same. 我也尝试过bytes=True但是行为是相同的。 Do others get the same result or is it some funny setting on my matplotlib that has gone awry? 其他人会得到相同的结果吗,还是我的matplotlib上的某个有趣的设置出现了问题?

Moreover - it seems this is happening in particular on the Accent colormap, but not necessarily others. 此外-似乎这在Accent颜色图上特别在发生,但不一定是其他情况。

In general, a colormap ranges between 0 and 1. In np.linspace(0,255,6) all values except the first are larger than 1, hence you get the output corresponding to the maximum value 1 for all but the first item of that list. 通常,颜色映射的范围是0到1。在np.linspace(0,255,6)除第一个值之外的所有值都大于1,因此对于该列表中除第一项以外的所有值,您都获得对应于最大值1的输出。 。

If instead you use numbers = np.linspace(0,1,6) , you will get 6 different values from that colormap. 相反,如果您使用numbers = np.linspace(0,1,6) ,则将从该颜色图中获得6个不同的值。

import matplotlib.pyplot as plt
import numpy as np

numbers = np.linspace(0,1,6)
newcm = plt.cm.Accent(numbers)
print(newcm)

produces 产生

[[ 0.49803922  0.78823529  0.49803922  1.        ]
 [ 0.74509804  0.68235294  0.83137255  1.        ]
 [ 1.          1.          0.6         1.        ]
 [ 0.21960784  0.42352941  0.69019608  1.        ]
 [ 0.74901961  0.35686275  0.09019608  1.        ]
 [ 0.4         0.4         0.4         1.        ]]

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

相关问题 如何使用matplotlib定义具有绝对值的colormap - how to define colormap with absolute values with matplotlib 带有 matplotlib 的颜色图 - Colormap with matplotlib 如何获取自定义色彩映射(matplotlib / python)中屏蔽值的透明度 - How to obtain transparency for masked values in customised colormap (matplotlib/python) 如何使用 matplotlib 以特定值定义颜色来创建线性颜色图? - How to create a linear colormap with color defined at specific values with matplotlib? 有没有办法使 matplotlib 中的颜色图上的非零值不同? - Is there a way to make non-zero values distinct on a colormap in matplotlib? Matplotlib:使用索引颜色值以发散颜色图在颜色条中居中颜色 - Matplotlib: Center colors in colorbar with diverging colormap using indexed color values matplotlib/seaborn 中罕见的大值偏向非线性颜色图 - Nonlinear colormap biased by rare large values in matplotlib/seaborn Python Matplotlib Colormap-标准化许多接近值的范围 - Python matplotlib Colormap - normalizing a range of many close values 有没有一种方法可以将标量值转换为matplotlib颜色图索引? - Is there a way to convert scalar values in an array to matplotlib colormap indices? 将连续色图中的颜色与matplotlib中的特定值相关联 - Associating colors from a continuous colormap to specific values in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM