gpt4 book ai didi

python - matplotlib:具有饱和度的单色颜色图

转载 作者:太空狗 更新时间:2023-10-30 00:07:43 25 4
gpt4 key购买 nike

如何创建线性颜色图,其中单个自定义 RGB 颜色的饱和度从 0 变为 1?

我需要像“Blues”或“Greens”这样的 map (参见此处:http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps),但需要自定义颜色。

我认为用 LinearSegmentedColormap 可能可以实现,但我不明白我需要如何设置参数。

最佳答案

请看下面的例子:

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

def CustomCmap(from_rgb,to_rgb):

# from color r,g,b
r1,g1,b1 = from_rgb

# to color r,g,b
r2,g2,b2 = to_rgb

cdict = {'red': ((0, r1, r1),
(1, r2, r2)),
'green': ((0, g1, g1),
(1, g2, g2)),
'blue': ((0, b1, b1),
(1, b2, b2))}

cmap = LinearSegmentedColormap('custom_cmap', cdict)
return cmap


fig, ax = plt.subplots(2,2, figsize=(6,6), subplot_kw={'xticks': [],'yticks': []})
fig.subplots_adjust(hspace=.1,wspace=.1)

ax = ax.ravel()

cmap1 = CustomCmap([0.00, 0.00, 0.00], [0.02, 0.75, 1.00]) # from black to +/- 5,192,255
cmap2 = CustomCmap([1.00, 1.00, 1.00], [0.02, 0.75, 1.00]) # from white to +/- 5,192,255
cmap3 = CustomCmap([1.00, 0.42, 0.04], [0.02, 0.75, 1.00]) # from +/- 255,108,10 to +/- 5,192,255
cmap4 = CustomCmap([1.00, 0.42, 0.04], [0.50, 0.50, 0.50]) # from +/- 255,108,10 to grey (128)


ax[0].imshow(np.random.rand(30,30), interpolation='none', cmap=cmap1)
ax[1].imshow(np.random.rand(30,30), interpolation='none', cmap=cmap2)
ax[2].imshow(np.random.rand(30,30), interpolation='none', cmap=cmap3)
ax[3].imshow(np.random.rand(30,30), interpolation='none', cmap=cmap4)

enter image description here

关于python - matplotlib:具有饱和度的单色颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16267143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com