簡體   English   中英

seaborn 熱圖中的自定義調色板間隔

[英]Custom color palette intervals in seaborn heatmap

我正在嘗試使用 seaborn 庫繪制熱圖

繪圖函數如下所示:

def plot_confusion_matrix(data, labels, **kwargs):
    """Visualize confusion matrix as a heat map."""
    col_map = kwargs.get('color_palette', sns.light_palette('navy', n_colors=5, as_cmap=False))

    sns.heatmap(
        vmin=0.0,
        vmax=1.0,
        data=data,
        cmap=col_map,
        xticklabels=labels,
        yticklabels=labels,
        linewidths=0.75,
    )

然而, data的直方圖看起來像這樣: 直方圖

現在我正在努力解決的問題是 seaborn 熱圖(查看波紋管)均勻地分割色標,因此大多數數據具有相同的顏色(因為數據分布不均勻)。

我無法找到如何為顏色級別設置某種間隔或邊界。

假設我有以下十六進制顏色值數組:

['#e5e5ff', '#acacdf', '#7272bf', '#39399f', '#000080']

有沒有辦法設置顏色,例如

[(threshold_0, hex_0), (threshold_1, hex_1), ..., (threshold_n, hex_n)]

其中threshold_i是 [0, 1) 范圍內的值


感謝任何幫助。

PS:用於說明的當前熱圖:

在此處輸入圖片說明

關於此處的文檔,您可以創建自己的顏色詞典。 這些 dicts 必須是 rgb 值,所以我寫了第一個測試函數來從 Hex_colors 和你想要的閾值生成一個:

def NonLinCdict(steps, hexcol_array):
    cdict = {'red': (), 'green': (), 'blue': ()}
    for s, hexcol in zip(steps, hexcol_array):
        rgb =matplotlib.colors.hex2color(hexcol)
        cdict['red'] = cdict['red'] + ((s, rgb[0], rgb[0]),)
        cdict['green'] = cdict['green'] + ((s, rgb[1], rgb[1]),)
        cdict['blue'] = cdict['blue'] + ((s, rgb[2], rgb[2]),)
    return cdict

hc = ['#e5e5ff', '#acacdf', '#7272bf', '#39399f', '#000080']
th = [0, 0.1, 0.5, 0.9, 1]

cdict = NonLinCdict(th, hc)
cm = mc.LinearSegmentedColormap('test', cdict)

plt.figure()
sns.heatmap(
        vmin=0.0,
        vmax=1.0,
        data=data,
        cmap=cm,
        linewidths=0.75)

它產生:

在此處輸入圖片說明

還可以做更多的事情(例如,針對離散跳轉,只需查看文檔...)但這應該可以回答您的原始問題-這次包括“自定義”...

但是,我必須補充一下我的個人意見:像這樣拉伸的顏色圖在這里可能會“令人愉悅”,但應該注意它們不會誤導觀眾的眼睛。

我希望這有幫助。

我能夠找到(在我看來不是很干凈)解決方案,它使用matplotlib.colors.LinearSegmentedColormap

代碼如下所示:

# NOTE: jupyter notebook mode
%matplotlib inline

import seaborn as sns
from matplotlib.colors import LinearSegmentedColormap

boundaries = [0.0, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0]  # custom boundaries

# here I generated twice as many colors, 
# so that I could prune the boundaries more clearly
hex_colors = sns.light_palette('navy', n_colors=len(boundaries) * 2 + 2, as_cmap=False).as_hex()
hex_colors = [hex_colors[i] for i in range(0, len(hex_colors), 2)]

colors=list(zip(boundaries, hex_colors))

custom_color_map = LinearSegmentedColormap.from_list(
    name='custom_navy',
    colors=colors,
)

 sns.heatmap(
        vmin=0.0,
        vmax=1.0,
        data=data,
        cmap=custom_color_map,
        xticklabels=labels,
        yticklabels=labels,
        linewidths=0.75,
  )

故意不解決您的問題中的“習慣” - 也許這在此期間有所幫助:

在眾所周知的在整個范圍內平滑變化的顏色圖下面,還有一些更適合顯示多個數據帶中的微小差異,例如gist_ncar

另見https://matplotlib.org/examples/color/colormaps_reference.html

在此處輸入圖片說明

創建於

sns.heatmap(vmin=0.0, vmax=1.0, data=data,  cmap='gist_ncar', linewidths=0.75)

暫無
暫無

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

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