我正在按照documentation创建自定义颜色图。我想要的地图如下所示:

python - pcolor的自定义颜色图-LMLPHP

这是我想出的字典:

   cdict = {'red':   ((0.00,  0.0, 0.0),
               (0.25,  1.0, 0.0),
               (0.50,  0.0, 0.0),
               (0.75,  1.0, 0.0),
               (1.00,  1.0, 0.0)),

     'green': ((0.00,  0.0, 0.0),
               (0.25, 1.0, 1.0),
               (0.50, 1.0, 1.0),
               (0.75, 1.0, 1.0),
               (1.00,  0.0, 0.0)),

     'blue':  ((0.00,  1.0, 1.0),
               (0.25,  0.0, 0.0),
               (1.00,  0.0, 0.0))
    }


但这并没有给我想要的结果。例如,值0.5使用红色呈现。

其余代码如下所示:

cmap = LinearSegmentedColormap('bgr', cdict)
plt.register_cmap(cmap=cmap)

plt.pcolor(dist, cmap='bgr')
plt.yticks(np.arange(0.5, len(dist.index), 1), dist.index)
plt.xticks(np.arange(0.1, len(dist.columns), 1), dist.columns, rotation=40)

for y in range(dist.shape[0]):
    for x in range(dist.shape[1]):
        plt.text(x + 0.5, y + 0.5, dist.iloc[y,x],
                 horizontalalignment='center',
                 verticalalignment='center', rotate=90
                 )
plt.show()


这是渲染的热图的示例:

python - pcolor的自定义颜色图-LMLPHP

我想念什么?

最佳答案

0.5在图中显示为红色的原因可能只是因为vminvmax都不是0.0和1.0。大多数matplotlib 2D绘图例程默认将vmax设置为数组中的最大值,在您的情况下看起来是0.53。如果要使0.5变为绿色,请将通话中的vmin=0.0, vmax=1.0设置为pcolor

您的色图字典几乎是正确的,但是由于您已经有了它,所以在0.25和0.75点处很难过渡到黄色/绿色,因此您应该将“

           (0.25,  1.0, 0.0),
           (0.50,  0.0, 0.0),
           (0.75,  1.0, 0.0),




           (0.25,  1.0, 1.0),
           (0.50,  0.0, 0.0),
           (0.75,  1.0, 1.0),


获得所需的色阶。结果如下:

python - pcolor的自定义颜色图-LMLPHP

10-08 00:26
查看更多