使用Python中的Matplotlib创建自定义颜色映射表

3
我有一张用matplotlib显示的图片。 enter image description here
这张图片是通过以下代码生成的:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm


labels = ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6']

data = np.array(
 [[ 0.000, 0.120, 0.043, 0.094, 0.037, 0.045],
  [ 0.120, 0.000, 0.108, 0.107, 0.105, 0.108],
  [ 0.043, 0.108, 0.000, 0.083, 0.043, 0.042],
  [ 0.094, 0.107, 0.083, 0.000, 0.083, 0.089],
  [ 0.037, 0.105, 0.043, 0.083, 0.000, 2.440],
  [ 0.045, 0.108, 0.042, 0.089, 2.440, 0.000]])


mask =  np.tri(data.shape[0], k=-1)
data = np.ma.array(data, mask=mask) # Mask out the lower triangle of data.

fig, ax = plt.subplots(sharex=True)
im = ax.pcolor(data, edgecolors='black', linewidths=0.3)

# Format
fig = plt.gcf()
fig.set_size_inches(10, 10)

ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)

# Turn off the frame.
ax.set_frame_on(False)
ax.set_aspect('equal')  # Ensure heatmap cells are square.

# Want a more natural, table-like display.
ax.invert_yaxis()
ax.yaxis.tick_right()
ax.xaxis.tick_top()

ax.set_xticklabels(labels, minor=False)
ax.set_yticklabels(labels, minor=False)

# Rotate the upper labels.
plt.xticks(rotation=90)
ax.grid(False)
ax = plt.gca()

for t in ax.xaxis.get_major_ticks():
    t.tick1On = False
    t.tick2On = False
for t in ax.yaxis.get_major_ticks():
    t.tick1On = False
    t.tick2On = False

fig.colorbar(im)

fig.savefig('out.png', transparent=False, bbox_inches='tight', pad_inches=0)

我想应用自定义色图,使值:
  • 在0-1之间的呈蓝白线性渐变
  • 在1-3之间的呈白红线性渐变。
非常感谢您的帮助。

1
请查看这个SO问题:https://dev59.com/x2Qn5IYBdhLWcg3wcWvM - mwaskom
2个回答

19

有多种方法可以做到这一点。在您的情况下,最简单的方法是使用LinearSegmentedColormap.from_list,并指定颜色的相对位置以及颜色名称。(如果您有均匀间隔的变化,则可以跳过元组,并只执行from_list('my cmap',['blue','white','red']))。然后,您需要手动指定数据的最小值和最大值(vminvmax关键字参数传递给imshow/pcolor/等)。

例如:

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

data = np.array(
             [[ 0.000, 0.120, 0.043, 0.094, 0.037, 0.045],
              [ 0.120, 0.000, 0.108, 0.107, 0.105, 0.108],
              [ 0.043, 0.108, 0.000, 0.083, 0.043, 0.042],
              [ 0.094, 0.107, 0.083, 0.000, 0.083, 0.089],
              [ 0.037, 0.105, 0.043, 0.083, 0.000, 2.440],
              [ 0.045, 0.108, 0.042, 0.089, 2.440, 0.000]])
mask = np.tri(data.shape[0], k=-1)
data = np.ma.masked_where(mask, data)

vmax = 3.0
cmap = LinearSegmentedColormap.from_list('mycmap', [(0 / vmax, 'blue'),
                                                    (1 / vmax, 'white'),
                                                    (3 / vmax, 'red')]
                                        )

fig, ax = plt.subplots()
im = ax.pcolor(data, cmap=cmap, vmin=0, vmax=vmax, edgecolors='black')
cbar = fig.colorbar(im)

cbar.set_ticks(range(4)) # Integer colorbar tick locations
ax.set(frame_on=False, aspect=1, xticks=[], yticks=[])
ax.invert_yaxis()

plt.show()

输入图像描述


2
这听起来像是地震色图
你可能想要强制设定最小值和最大值,使得中间部分为白色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接