首页 > 解决方案 > 如何在 matplotlib 中获得更轻的“喷射”颜色图

问题描述

我正在尝试为集群制作背景颜色,但不知道如何使喷射颜色图更亮或更暗。有人可以帮助我吗?

标签: pythonmatplotlibscatter-plotcolormapimshow

解决方案


这肯定取决于您如何定义“较浅”或“较深”。一个有用的定义是在 HSL 空间中乘以颜色的亮度通道。这可能看起来像

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import colorsys


def man_cmap(cmap, value=1.):
    colors = cmap(np.arange(cmap.N))
    hls = np.array([colorsys.rgb_to_hls(*c) for c in colors[:,:3]])
    hls[:,1] *= value
    rgb = np.clip(np.array([colorsys.hls_to_rgb(*c) for c in hls]), 0,1)
    return mcolors.LinearSegmentedColormap.from_list("", rgb)

cmap = plt.cm.get_cmap("jet")

fig, (ax1, ax2, ax3) = plt.subplots(3)
x=np.linspace(0,1,64)

sc = ax1.scatter(x,np.ones_like(x), c=x, cmap=cmap)
fig.colorbar(sc, ax=ax1, orientation="horizontal")

sc = ax2.scatter(x,np.ones_like(x), c=x, cmap=man_cmap(cmap, 0.75))
fig.colorbar(sc, ax=ax2, orientation="horizontal")

sc = ax3.scatter(x,np.ones_like(x), c=x, cmap=man_cmap(cmap, 1.25))
fig.colorbar(sc, ax=ax3, orientation="horizontal")

plt.show()

在此处输入图像描述


推荐阅读