简体   繁体   English

如何使用matplotlib定义具有绝对值的colormap

[英]how to define colormap with absolute values with matplotlib

I use the following script for plotting: 我使用以下脚本进行绘图:

import matplotlib

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
import math
import matplotlib as mpl
from matplotlib.ticker import MultipleLocator
from matplotlib.colors import LinearSegmentedColormap

cdict1 = {'red':   ((0.0, 1.0, 1.0),
                   (0.4, 1.0, 1.0),
                   (0.7, 0.0, 0.0),
                   (1.0, 0.0, 0.0)),

         'green': ((0.0, 1.0, 1.0),
                   (0.1, 0.0, 0.0),
                   (1.0, 0.0, 0.0)),

         'blue':   ((0.0, 1.0, 1.0),
                   (0.1, 0.0, 0.0),
                   (0.4, 0.0, 0.0),
                   (1.0, 1.0, 1.0))
        }

white_blue_red = LinearSegmentedColormap('WhiteBlueRed', cdict1)
plt.register_cmap(cmap=white_blue_red)

x = np.loadtxt('data.dat',
                 unpack=True)

plt.scatter(x[0], x[1], marker='.', s=3, linewidths=0, c=x[3], cmap= \
            plt.get_cmap('WhiteBlueRed')) # plt.cm.bwr  
plt.colorbar()

plt.show()

The colormap I have defined uses relative values (0 minimum value of function 1 maximum value). 我定义的色彩映射使用相对值(函数1最大值的0最小值)。 the problem is that I want to use that code for plotting hundreds of different files and I want that each plot has the exact same colormap. 问题是我想使用该代码绘制数百个不同的文件,我希望每个绘图具有完全相同的色彩映射。 Is there the possibility to define colormaps with absolute values? 是否有可能用绝对值定义色彩图? That would solve my problem. 这将解决我的问题。

The key in this case is the norm , not the colormap. 在这种情况下,关键是norm ,而不是色彩映射。

The colormap defines colors for already scaled data. colormap定义已缩放数据的颜色。 The norm scales the data to a 0-1 range. norm将数据缩放到0-1范围。

By default, a Normalize instance will be created that scales between the min and max of the data or the vmin and vmax kwargs, if they are supplied. 默认情况下,将创建一个Normalize实例,该实例可在数据的最小值和最大值之间 vminvmax kwargs之间进行缩放(如果提供的话)。

However, there are a few different helper functions that may be useful in your case. 但是,有一些不同的辅助函数可能对您的情况有用。

If you want a discrete color bar, there's a helper function to generate both a norm and a cmap for you: matplotlib.colors.from_levels_and_colors It takes a list of values and a list of colors and returns a BoundaryNorm instance and a LinearSegmentedColormap instance: 如果你想要一个离散的颜色条,有一个辅助函数可以为你生成一个normcmapmatplotlib.colors.from_levels_and_colors它获取一个值列表和一个颜色列表,并返回一个BoundaryNorm实例和一个LinearSegmentedColormap实例:

For example: 例如:

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

data1 = 3 * np.random.random((10, 10))
data2 = 5 * np.random.random((10, 10))

levels = [0, 1, 2, 3, 4, 5]
colors = ['red', 'brown', 'yellow', 'green', 'blue']
cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors)

fig, axes = plt.subplots(ncols=2)
for ax, dat in zip(axes, [data1, data2]):
    im = ax.imshow(dat, cmap=cmap, norm=norm, interpolation='none')
    fig.colorbar(im, ax=ax, orientation='horizontal')
plt.show()

在此输入图像描述

Note that this creates a discrete colormap. 请注意,这会创建一个离散的色彩映射。

If we wanted to use a continuous colormap instead, we can either specify the same vmin and vmax arguments or create our own Normalize instance and pass it in as the norm argument for all images. 如果我们想要使用连续的色彩映射,我们可以指定相同的vminvmax参数,也可以创建我们自己的Normalize实例,并将其作为所有图像的norm参数传递。

Also, there's a similar function to create a continuous colormap from a list of colors: 此外,还有一个类似的功能,可以从颜色列表中创建连续的色彩映射:

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

data1 = 3 * np.random.random((10, 10))
data2 = 5 * np.random.random((10, 10))

colors = ['red', 'brown', 'yellow', 'green', 'blue']
cmap = LinearSegmentedColormap.from_list('name', colors)
norm = plt.Normalize(0, 5)

fig, axes = plt.subplots(ncols=2)
for ax, dat in zip(axes, [data1, data2]):
    im = ax.imshow(dat, cmap=cmap, norm=norm, interpolation='none')
    fig.colorbar(im, ax=ax, orientation='horizontal')
plt.show()

在此输入图像描述

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

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