如何在 matplotlib 中创建自定义发散色图?

How to create a custom diverging colormap in matplotlib?

我想在 matplotlib 中创建一个类似于“RdBu”的颜色图。

我想按照这个顺序制作颜色图浅蓝色->深蓝色->黑色(中心)->深红色->浅红色。像这样的东西。

所以它类似于“RdBu”,但白色变为黑色,深色与浅色互换。 所以它只是反转了“RdBu”的颜色。 我不知道怎么做。

我最近刚尝试创建一个颜色图来满足我的要求。这是我尝试构建您需要的颜色图。我知道它并不完美。但它告诉你如何开始。

import matplotlib
import matplotlib.cm as cm
from matplotlib.colors import Normalize
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

# create sample data set
# both will be: 0 - 1
x = np.random.rand(400)
y = np.random.rand(400)
# for realistic use
# set extreme values -900, +900 (approx.)
rval = 900
z = ((x+y)-1)*rval

# set up fig/ax for plotting
fig, ax = plt.subplots(figsize=(5, 5))

# option: set background color
ax.set_facecolor('silver')

# the colormap to create
low2hiColor = None

# create listedColormap
bottom = cm.get_cmap('Blues', 256)
top = cm.get_cmap('Reds_r', 256)
mycolormap = np.vstack((bottom(np.linspace(0.25, 1, 64)),
                        np.array([
                        [0.03137255, 0.08823529, 0.41960784, 1.],
                        [0.02137255, 0.04823529, 0.21960784, 1.],
                        [0.01137255, 0.02823529, 0.11960784, 1.],
                        [0.00037255, 0.00823529, 0.00960784, 1.],
                        #[0.00000255, 0.00000529, 0.00060784, 1.],
                        ])
                       ))
mycolormap = np.vstack((mycolormap,
                        np.array([
                        #[0.00060784, 0.00000529, 0.00000255, 1.],
                        [0.00960784, 0.00823529, 0.00037255, 1.],
                        [0.11960784, 0.02823529, 0.01137255, 1.],
                        [0.21960784, 0.04823529, 0.02137255, 1.],
                        [0.41960784, 0.08823529, 0.03137255, 1.],
                        ])
                       ))
mycolormap = np.vstack((mycolormap,
                        top(np.linspace(0, 0.75, 64)),
                       ))

low2hiColor = ListedColormap(mycolormap, name='low2hiColor')

# colorbar is created separately using pre-determined `cmap`
minz = -900 #min(z)
maxz = 900  #max(z)
norm_low2hiColor = matplotlib.colors.Normalize(minz, maxz)

# plot dataset as filled contour
norm1 = matplotlib.colors.Normalize(minz, maxz)
cntr1 = ax.tricontourf(x, y, z, levels=64, cmap=low2hiColor, norm=norm1)

gridlines = ax.grid(b=True)  # this plot grid

cbar= plt.colorbar( cntr1 ) 
plt.title("Light-Dark Blue Black Dark-Light Red")
plt.show()

样本图:

我制作了一个简单的工具来帮助创建颜色图并生成所需的代码:

https://eltos.github.io/gradient/#4C71FF-0025B3-000000-C7030D-FC4A53

Screenshot

-->

以及您从下载按钮获得的代码:

#!/usr/bin/env python

from matplotlib.colors import LinearSegmentedColormap

my_gradient = LinearSegmentedColormap.from_list('my_gradient', (
                 # Edit this gradient at https://eltos.github.io/gradient/#4C71FF-0025B3-000000-C7030D-FC4A53
                 (0.000, (0.298, 0.443, 1.000)),
                 (0.250, (0.000, 0.145, 0.702)),
                 (0.500, (0.000, 0.000, 0.000)),
                 (0.750, (0.780, 0.012, 0.051)),
                 (1.000, (0.988, 0.290, 0.325))))


if __name__ == '__main__':
    import numpy as np
    from matplotlib import pyplot as plt
    
    plt.imshow([np.arange(1000)], aspect="auto", cmap=my_gradient)
    plt.show()

我想通过简单地组合现有的颜色图来创建不同的颜色图。

代码如下:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors
from typing import List, Tuple

def get_hex_col(cmap) -> List[str]:
    """Return list of hex colors for cmap"""
    return [colors.rgb2hex(cmap(i)) for i in range(cmap.N)]

def get_cmap_list(
        cmap_name: str, length_n: int) -> [str]:
    """Create a classified colormap of length N
    """
    cmap = plt.cm.get_cmap(cmap_name, length_n)
    cmap_list = get_hex_col(cmap)
    return cmap_list

def get_diverging_colormap(
        cmap_diverging:Tuple[str,str], color_count: int = k_classes):
    """Create a diverging colormap from two existing with k classes"""
    div_cmaps: List[List[str]] = []
    for cmap_name in cmap_diverging:
        cmap_list = get_cmap_list(
            cmap_name, length_n=color_count)
        div_cmaps.append(cmap_list)
    div_cmaps[1] = list(reversed(div_cmaps[1]))
    cmap_nodata_list = div_cmaps[1] + div_cmaps[0]
    return colors.ListedColormap(cmap_nodata_list)

# apply
cmaps_diverging: Tuple[str] = ("OrRd", "Purples")
cmap = get_diverging_colormap(cmaps_diverging)

# visualize
def display_hex_colors(hex_colors: List[str]):
    """Visualize a list of hex colors using pandas"""
    df = pd.DataFrame(hex_colors).T
    df.columns = hex_colors
    df.iloc[0,0:len(hex_colors)] = ""
    display(df.style.apply(lambda x: apply_formatting(x, hex_colors)))

display_hex_colors(cmap.colors)

例如“OrRd”和“Purples”的输出: