如何使用pcolormesh获得对数自定义色阶?

时间:2018-08-08 08:06:51

标签: python matplotlib

所以我试图用pcolormesh绘制热图,并且我有带有值的数据。该数据为0-500,但我希望色标本质上是对数的。基本上应该看起来像这样: enter image description here

热图数据位于pcolormesh中使用的(x,y,data)中。 这是我为此精确比例写出的cmap:

cdict1 = {
    'red': ((0.0, 1.0, 1.0),
            (0.16, 237, 237),
            (0.24, 205, 205),
            (0.32, 153, 153),
            (0.40, 83, 83),
            (0.48, 50, 50),
            (0.56, 50, 50),
            (0.64, 5, 5),
            (0.72, 5, 5),
            (0.80, 10, 10),
            (0.88, 44, 44),
            (1, 44, 44)),

    'green': ((0.0, 1.0, 1.0),
            (0.16, 237, 237),
            (0.24, 255, 255),
            (0.32, 240, 240),
            (0.40, 189, 189),
            (0.48, 166, 166),
            (0.56, 150, 150),
            (0.64, 112, 112),
            (0.72, 80, 80),
            (0.80, 31, 31),
            (0.88, 1, 1),
            (1, 1, 1)),

    'blue': ((0.0, 1.0, 1.0),
            (0.16, 237, 237),
            (0.24, 205, 205),
            (0.32, 178, 178),
            (0.40, 159, 159),
            (0.48, 150, 150),
            (0.56, 180, 180),
            (0.64, 176, 176),
            (0.72, 140, 140),
            (0.80, 150, 150),
            (0.88, 70, 70),
            (1, 70, 70)),
}
cm_rgb = LinearSegmentedColormap('bgr', cdict1)
cs = m.pcolormesh(x, y, data, vmin=0, vmax=50e-8,shading='flat', cmap=cm_rgb)
tick_levels = [0, 2e-8, 3e-8, 4e-8, 5e-8, 6e-8, 8e-8, 10e-8, 15e-8, 20e-8, 30e-8, 50e-8]
bar = plt.colorbar(cs, orientation='vertical', shrink=0.5)
bar.set_ticks(tick_levels)
plt.show()

运行此命令时,没有显示颜色。没错。

基本上,我的问题是,如何将所有这些放在一起,以便我的热图使用自定义的比例尺和刻度线?

1 个答案:

答案 0 :(得分:1)

我不太了解字典是如何产生所显示颜色的。无论如何,似乎只使用这些颜色的列表就容易得多。

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


x,y = np.meshgrid(np.arange(11), np.arange(11))
data = np.linspace(0,50e-8,100).reshape(10,10)


colors = [(255,255,255),(237,250,194),(205,255,205),
          (153,240,178),(83,189,159),(50,166,150),
          (50,150,180),(5,112,176),(5,80,140),
          (10,31,150),(44,1,70)]

cm_rgb = LinearSegmentedColormap.from_list('', np.array(colors)/255.)
cs = plt.pcolormesh(x, y, data, vmin=0, vmax=50e-8, shading='flat', cmap=cm_rgb)
tick_levels = [0, 2e-8, 3e-8, 4e-8, 5e-8, 6e-8, 8e-8, 10e-8, 15e-8, 20e-8, 30e-8, 50e-8]
bar = plt.colorbar(cs, orientation='vertical', shrink=0.5)
bar.set_ticks(tick_levels)
plt.show()

enter image description here

相关问题