如何创建自定义的颜色图并将其用于不同的数据范围? [英] How to create a customized colormap and use it for different ranges of data?

查看:116
本文介绍了如何创建自定义的颜色图并将其用于不同的数据范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我有这样的数据:

  import numpy as np 
import matplotlib.pyplot as plt
导入matplotlib.colors

#出于可复制目的
np.random.seed(0)

#生成一些数据
n = 30
x = np.array(range(n))

a1 = np.random.rand(n)
a2 = a1 * 100

,我想将这些数据绘制在两个子图中,我可以这样做(



我遇到的问题是,尽管右边的值大100倍,但现在看起来这些数据是相同的。 / p>

因此,我想要的是一个可以用于两个图的色图;而不是

  matplotlib.colors.LinearSegmentedColormap.from_list(,[(0.,'#9696ff'),(0.2 ,'#f0ffff'),(1.0,'#ff0000')])

我想使用类似

  min_a1_a2 = min(min(a1),min(a2))
max_a1_a2 = max(max (a1),max(a2))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(,[(min_a1_a2,'#9696ff'),(((min_a1_a2 + max_a1_a2)/ 2.,'#f0ffff' ),(max_a1_a2,'#ff0000')])

但这总是会导致错误


ValueError:数据映射点必须以x = 0开头。并以x = 1


结尾,当我将其传递给 scatter (使用



是否可以将任意(值,颜色)元组传递给 matplotlib .colors.LinearSegmentedColormap.from_list ,然后将生成的颜色映射到例如分散,如果是这样的话,怎么办?

解决方案

一个matplotlib colormap将0到1之间的数字范围映射到颜色范围。



如果数据的间隔范围不是[0,1](当然几乎总是这样),则首先将其标准化为该间隔。归一化由使用中的ScalarMappable内部完成(例如,在这种情况下为散点图)。



在需要自定义规范化的情况下,例如当两个不同的图需要共享颜色编码时,可以在创建ScalarMappable时指定。



使用 vmin vmax

  plt.scatter(x,y,c = c,cmap = cmap,vmin = 0,vmax = 100)

或通过规范化实例

  plt.scatter( x,y,c = c,cmap = cmap,norm = plt.Normalize(0,100))


Let's say I have data like this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

# for reproducibility purposes
np.random.seed(0)

# generate some data
n = 30
x = np.array(range(n))

a1 = np.random.rand(n)
a2 = a1 * 100

and I want to plot these data in two subplots, I can do (variation of this answer)

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", [(0., '#9696ff'), (0.2, '#f0ffff'), (1.0, '#ff0000')])

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, a1, c=a1, cmap=cmap)
ax2.scatter(x, a2, c=a2, cmap=cmap)
plt.show()

which gives

The problem I have is that it now looks like these data are identical although the values on the right are 100 times larger.

So, what I would like to have is a colormap which I can use for both plots; instead of

matplotlib.colors.LinearSegmentedColormap.from_list("", [(0., '#9696ff'), (0.2, '#f0ffff'), (1.0, '#ff0000')])

I would like to use something like

min_a1_a2 = min(min(a1), min(a2))
max_a1_a2 = max(max(a1), max(a2))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", [(min_a1_a2, '#9696ff'), ((min_a1_a2 + max_a1_a2) / 2., '#f0ffff'), (max_a1_a2, '#ff0000')])

but this always results in the error

ValueError: data mapping points must start with x=0. and end with x=1

when I pass it to scatter (using the same commands as above).

Is there a way to pass arbitrary (value, color) tuples to matplotlib.colors.LinearSegmentedColormap.from_list and then the resulting colormap to e.g. scatter and if so how would one do this?

解决方案

A matplotlib colormap maps the numerical range between 0 and 1 to a range of colors.

If the data ranges over an interval other than [0,1] (which is almost always the case of course), one would normalize to that interval first. This normalization is done internally by the ScalarMappable in use (e.g. the scatter plot in this case).

In cases where a custom normalization is needed, as when two different plots need to share a colorcoding, this can be specified when creating the ScalarMappable.

Either using vmin and vmax

plt.scatter(x,y, c=c, cmap=cmap, vmin=0, vmax=100)

or via a Normalization instance

plt.scatter(x,y, c=c, cmap=cmap, norm=plt.Normalize(0,100))

这篇关于如何创建自定义的颜色图并将其用于不同的数据范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆