简体   繁体   English

Matplotlib颜色图范围

[英]Matplotlib colormap range

I'm trying to understand how matplotlib colormaps work. 我试图了解matplotlib颜色图如何工作。 Consider the following code: 考虑以下代码:

import matplotlib.pyplot as plt
cmap = plt.get_cmap('jet')
print cmap(200)

which prints 哪个打印

(1.0, 0.46550472040668145, 0.0, 1.0)

So my understanding is that a colormap maps a numerical value (200 in this case) to a color value (1.0, 0.46550472040668145, 0.0, 1.0 in this case). 所以我的理解是,颜色图将数值(在这种情况下为200)映射到颜色值(在这种情况下为1.0、0.46550472040668145、0.0、1.0)。 How does matplotlib set the range for its colormap? matplotlib如何设置其颜色图的范围?

Is it possible to define a maximum and a minimum value between which a linear map is applied? 是否可以定义应用线性图的最大值和最小值? With imshow() one can set a vmin and a vmax parameter, however, I would have to do it at the colormap level because I'm providing the colormap to another function later on. 使用imshow()可以设置一个vmin和一个vmax参数,但是,我将不得不在色图级别进行此操作,因为稍后将色图提供给另一个函数。

This might be a more general question on how to colormaps work; 这可能是关于颜色表如何工作的更一般性的问题。 in seaborn 's color palettes, for example, there is no option for the range either. 例如,在seaborn的调色板中,该范围也没有选择。

The range of colormaps is always between 0 and 1. You will need to normalize your data to this range. 色彩图的范围始终在0到1之间。您需要将数据标准化到该范围。 For example, to map the range between 0 and 400 linearly to the colors of a colormap, 例如,要将0到400之间的范围线性映射到颜色图的颜色,

import matplotlib.pyplot as plt
cmap = plt.get_cmap('viridis')
norm = plt.Normalize(0, 400)

color = cmap(norm(200.))

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

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