gpt4 book ai didi

Matplotlib 较暗的 HSV 颜色图

转载 作者:行者123 更新时间:2023-12-02 20:23:13 29 4
gpt4 key购买 nike

我正在使用 matplotlib 中的 HSV 颜色图来绘制一些向量场。有没有办法使 HSV 颜色变暗或变得更平滑,使它们看起来更像这样

enter image description here

比我原来的绘图颜色太亮:

enter image description here

最佳答案

简介

假设您正在尝试绘制这样的 pcolor 图像:

import numpy as np
import matplotlib.pyplot as plt

y, x = np.mgrid[slice(-3, 3 + 0.05, 0.05),
slice(-3, 3 + 0.15, 0.15)]
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]

fig = plt.figure(1)
fig.clf()
ax = plt.gca()
pcol = ax.pcolormesh(x, y, z, cmap=plt.get_cmap('hsv'), )
plt.colorbar(pcol)
ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])

您的图像将是:

HSV colormap.

方法

我编写了 MPL cookbook cmap_map function 的替代实现修改颜色图。除了支持 kwargs 和 pep8 合规性之外,此版本还处理颜色图中的不连续性:

import numpy as np
from matplotlib.colors import LinearSegmentedColormap as lsc


def cmap_map(function, cmap, name='colormap_mod', N=None, gamma=None):
"""
Modify a colormap using `function` which must operate on 3-element
arrays of [r, g, b] values.

You may specify the number of colors, `N`, and the opacity, `gamma`,
value of the returned colormap. These values default to the ones in
the input `cmap`.

You may also specify a `name` for the colormap, so that it can be
loaded using plt.get_cmap(name).
"""
if N is None:
N = cmap.N
if gamma is None:
gamma = cmap._gamma
cdict = cmap._segmentdata
# Cast the steps into lists:
step_dict = {key: map(lambda x: x[0], cdict[key]) for key in cdict}
# Now get the unique steps (first column of the arrays):
step_list = np.unique(sum(step_dict.values(), []))
# 'y0', 'y1' are as defined in LinearSegmentedColormap docstring:
y0 = cmap(step_list)[:, :3]
y1 = y0.copy()[:, :3]
# Go back to catch the discontinuities, and place them into y0, y1
for iclr, key in enumerate(['red', 'green', 'blue']):
for istp, step in enumerate(step_list):
try:
ind = step_dict[key].index(step)
except ValueError:
# This step is not in this color
continue
y0[istp, iclr] = cdict[key][ind][1]
y1[istp, iclr] = cdict[key][ind][2]
# Map the colors to their new values:
y0 = np.array(map(function, y0))
y1 = np.array(map(function, y1))
# Build the new colormap (overwriting step_dict):
for iclr, clr in enumerate(['red', 'green', 'blue']):
step_dict[clr] = np.vstack((step_list, y0[:, iclr], y1[:, iclr])).T
return lsc(name, step_dict, N=N, gamma=gamma)

实现

要使用它,只需定义一个函数来修改您的 RGB colors根据您的喜好(值从 0 到 1)并将其作为输入提供给 cmap_map。例如,要使颜色接近您提供的图像中的颜色,您可以定义:

def darken(x, ):
return x * 0.8

dark_hsv = cmap_map(darken, plt.get_cmap('hsv'))

然后修改对pcolormesh的调用:

pcol = ax.pcolormesh(x, y, z, cmap=dark_hsv)

Darker HSV.

如果您只想使图像中的绿色变暗,您可以这样做(现在全部在一行中):

pcol = ax.pcolormesh(x, y, z,
cmap=cmap_map(lambda x: x * [1, 0.7, 1],
plt.get_cmap('hsv'))
)

Darken green only

关于Matplotlib 较暗的 HSV 颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31321967/

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