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

2024-05-19 10:53:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在matplotlib中创建一个类似于“RdBu”的颜色映射。 traditional "RdBu"

我想让这个序列中的颜色映射为浅蓝色->;深蓝色->;黑色(中间)——>;深红色->;浅红色。像这样的。 Required colormap

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


Tags: gtmatplotlib颜色序列黑色红色白色深色
2条回答

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

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()

样地:

b-k-r

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

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()

相关问题 更多 >

    热门问题