色彩图和颜色条在指定范围内被压缩
这是一个示例代码。我想制作一个热力图,数值范围限制在0.5到0.6之间,但颜色的变化是线性的,范围是[(0, 'yellow'), (0.25, 'orange'), (1, 'darkred')]
。不过,由于没有接近0和1的值,所以颜色的变化被压缩到了0.5到0.6之间(下面的图片展示了这一点)。
import numpy as np
import matplotlib.pyplot as plt
# Define the range of values for x and y axes
xs = np.linspace(0.1, 0.9, 100)
ys = np.linspace(0, 1, 100)
# Initialize an array to store phases
phases = np.zeros((len(ys), len(xs)))
# Generate random phases for each combination of x and y
for i, y in enumerate(ys):
for j, x in enumerate(xs):
phases[i, j] = np.random.uniform(0.5, 0.6)
# Plot the phases as colors
colors = [(0, 'yellow'), (0.25, 'orange'), (1, 'darkred')]
cmap = plt.cm.colors.LinearSegmentedColormap.from_list('cphase', colors)
fig, ax = plt.subplots()
im = ax.pcolormesh(xs, ys, phases, cmap=cmap)
# Set axes information that is common across plots
ax.set(xlim=[0.1, 0.9], ylim=[0, 1])
ax.set_box_aspect(1)
# Add a color bar with ticks at 0, 0.5, and 1
cbar = plt.colorbar(im)
cbar.set_ticks([0, 0.5, 1])
plt.show()
这是上面代码块的输出结果。
现在,如果我手动将phases[0][0]
改为0,phases[99][99]
改为1,图表看起来就是这样的(这是我希望在不手动更改的情况下达到的效果)。
0 个回答
暂无回答