python - 如何在 matplotlib 中获得更轻的“喷射”颜色图
问题描述
我正在尝试为集群制作背景颜色,但不知道如何使喷射颜色图更亮或更暗。有人可以帮助我吗?
解决方案
这肯定取决于您如何定义“较浅”或“较深”。一个有用的定义是在 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()
推荐阅读
- reactjs - 如何使用 React Material UI 组件对我的用户进行身份验证?
- c++ - 避免 Windows 模态循环
- javascript - 如何在没有日期的情况下解析当地时间?
- asp.net-mvc - 数据库表列中值的下拉列表 - MVC 实体框架
- reactjs - 使用反应路由器访问通配符路径的参数
- java - SerializationException:反序列化 Avro 消息时出错 (StringIndexOutOfBoundsException)
- java - 我在 Java 中的数学方法似乎只是偶尔工作,但以重复的模式
- javascript - 如何防止网络上的屏幕截图?[2020]
- int - 你如何增加从 llength 返回的数字?
- c++ - C++ 编译器抱怨没有默认构造函数