簡體   English   中英

matplotlib colormap:不要調整大小

[英]matplotlib colormap: do not resize

我正在使用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)]

但matplotlib似乎會自動調整顏色變化范圍:左下方網格應為淺灰色,因為其對應值為0.2而不是0.0。 同樣,右下方網格應為深灰色,因為它是0.8而不是1。

我想我錯過了任命顏色映射動態范圍的步驟。 我對matplotlib的文檔進行了一些研究,但沒有找到我想要的東西。

要顯式設置顏色映射范圍,您需要使用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)

在此輸入圖像描述

在這里查看更多信息: http//matplotlib.org/api/cm_api.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM