如何在不均匀的色阶上将0设置为白色?

问题描述 投票:1回答:1

我的色阶不均匀,我希望0为白色。所有负色都必须为蓝色,所有正色都必须为红色。我目前的尝试是显示0蓝色和0.7 wite。

反正有将0设置为白色吗?

import matplotlib.colors as colors 


bounds_min = np.arange(-2, 0, 0.1)
bounds_max = np.arange(0, 4.1, 0.1)
bounds = np.concatenate((bounds_min, bounds_max), axis=None)       
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)      # I found this in the internet and thought this would solve my problem. But it dosn't...
m.pcolormesh(xx, yy, interpolated_grid_values, norm=norm, cmap='RdBu_r')
python matplotlib plot colors matplotlib-basemap
1个回答
0
投票

您可以尝试在负片和正片上设置相同数量的颜色。

这里是带有生成数据的演示代码。 zz被选择为围绕中心旋转的正弦,并且缩放为从-2到4,因此围绕1对称。在左侧以默认颜色显示图像。在右侧,更改了颜色,将白色设置为零。

由于所有正值均为红色,因此右图中的红色条带比蓝色的宽。在左侧图像中,条带具有相等的宽度。颜色栏指示零为白色。

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

x = np.linspace(-20, 20, 500)
y = np.linspace(-20, 20, 500)
xx, yy = np.meshgrid(x, y)
zz = np.sin(np.sqrt(xx * xx + yy * yy)) * 3 + 1

bounds_min = np.linspace(-2, 0, 128)
bounds_max = np.linspace(0, 4, 128)[1:] # the zero is only needed once
bounds = np.concatenate((bounds_min[:-1], bounds_max), axis=None)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
mesh1 = ax1.pcolormesh(xx, yy, zz, cmap='RdBu_r')
ax1.set_aspect('equal')
ax1.set_title('using a standard coloring')
fig.colorbar(mesh1, ax=ax1)

mesh2 = ax2.pcolormesh(xx, yy, zz, norm=norm, cmap='RdBu_r')
ax2.set_aspect('equal')
ax2.set_title('using a coloring with white at zero')
ticks = np.concatenate([(np.arange(-2.0, 0, 0.25), np.arange(0, 4.0, 0.5))])
fig.colorbar(mesh2, ax=ax2, ticks=ticks)

plt.show()

example plot

© www.soinside.com 2019 - 2024. All rights reserved.