Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom color palette intervals in seaborn heatmap

I am trying to plot a heatmap using seaborn library.

The plotting function looks like this:

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,
    )

The histogram of the data, however, looks like this: Histogram

Now the issue I am struggling with is that seaborn heatmap(view bellow) splits evenly the color scale and hence most of the data has the same color (since the data is not evenly distributed).

I was not able to find out how to set some sort of intervals or boundaries for the color levels.

Suppose I have the following array of hex color values:

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

Is there a way to set up a color such as

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

where threshold_i is a value in range [0, 1)


Appreciate any help.

PS: current heatmap for illustration:

enter image description here

like image 794
CermakM Avatar asked Jan 03 '23 10:01

CermakM


1 Answers

Knowingly not addressing the "custom" in your question - perhaps this helps in the meantime:

Beneath well known colormaps which change smoothly over the whole range, there are also a few which are suited better to show small differences in several bands of data, gist_ncar for example.

See also https://matplotlib.org/examples/color/colormaps_reference.html

enter image description here

created with

sns.heatmap(vmin=0.0, vmax=1.0, data=data,  cmap='gist_ncar', linewidths=0.75)
like image 168
SpghttCd Avatar answered Jan 13 '23 19:01

SpghttCd