简体   繁体   English

matplotlib colormap:不要调整大小

[英]matplotlib colormap: do not resize

I am drawing a confusion matrix using matplotlib: 我正在使用matplotlib绘制一个混淆矩阵:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

conf_arr_hs = [[90, 74],
                [33, 131]]
norm_conf_hs = []
for i in conf_arr_hs:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf_hs.append(tmp_arr)


confmatmap=cm.binary    
fig = plt.figure()


plt.clf()
ax = fig.add_subplot(111)
res = ax.imshow(np.array(norm_conf_hs), cmap=confmatmap, interpolation='nearest')
for x in xrange(2):
    for y in xrange(2):
        textcolor = 'black'
        if norm_conf_hs[x][y] > 0.5:
            textcolor = 'white'
        ax.annotate("%0.2f"%norm_conf_hs[x][y], xy=(y, x),  horizontalalignment='center', verticalalignment='center', color=textcolor)]

But matplotlib seems to auto-resize the color change range: the bottom left grid should be light gray since its corresponding value is 0.2 instead of 0.0. 但matplotlib似乎会自动调整颜色变化范围:左下方网格应为浅灰色,因为其对应值为0.2而不是0.0。 Similarly, bottom right grid should be dark gray since it is 0.8 instead of 1. 同样,右下方网格应为深灰色,因为它是0.8而不是1。

I think I miss the step of appointing the dynamic range for color mapping. 我想我错过了任命颜色映射动态范围的步骤。 I did some research into the documentation of matplotlib but did not find what I want. 我对matplotlib的文档进行了一些研究,但没有找到我想要的东西。

To explicitly set the color map range, you want to use the set_clim command: 要显式设置颜色映射范围,您需要使用set_clim命令:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.ion()

conf_arr_hs = [[90, 74],
                [33, 131]]
norm_conf_hs = []
for i in conf_arr_hs:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf_hs.append(tmp_arr)

confmatmap=plt.cm.binary    
fig = plt.figure()

plt.clf()
ax = fig.add_subplot(111)
res = ax.imshow(np.array(norm_conf_hs), cmap=confmatmap, interpolation='nearest')
res.set_clim(0,1) # set the limits for your color map

for x in xrange(2):
    for y in xrange(2):
        textcolor = 'black'
        if norm_conf_hs[x][y] > 0.5:
            textcolor = 'white'
        ax.annotate("%0.2f"%norm_conf_hs[x][y], xy=(y, x),  horizontalalignment='center', verticalalignment='center', color=textcolor)

在此输入图像描述

check more our here: http://matplotlib.org/api/cm_api.html 在这里查看更多信息: http//matplotlib.org/api/cm_api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM