How to invert color of seaborn heatmap colorbar

前端 未结 4 1292
闹比i
闹比i 2021-01-30 23:10

I use an heatmap to visualize a confusion matrix. I like the standard colors, but I would like to have 0s in light orange and highest values in dark purple.

I managed to

相关标签:
4条回答
  • 2021-01-30 23:20

    Did you try to invert the colormap?

    sns.cubehelix_palette(as_cmap=True, reverse=True)
    
    0 讨论(0)
  • 2021-01-30 23:22

    we can now quickly achieve reverse color just by putting _r in the end.

    For example: for viridis => viridis_r

    sns.heatmap(corr_matrix, annot=True, cmap='viridis_r');
    
    0 讨论(0)
  • 2021-01-30 23:23

    The default cmap is sns.cm.rocket. To reverse it set cmap to sns.cm.rocket_r

    Using your code:

    cmap = sns.cm.rocket_r
    
    ax = sns.heatmap(cm_prob,
                     annot=False,
                     fmt=".3f",
                     xticklabels=print_categories,
                     yticklabels=print_categories,
                     vmin=-0.05,
                     cmap = cmap)
    
    0 讨论(0)
  • 2021-01-30 23:36

    To expand on Ben's answer, you can do this with most if not any color map.

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    X = np.random.random((4, 4))
    sns.heatmap(X,cmap="Blues")
    plt.show()
    sns.heatmap(X,cmap="Blues_r")
    plt.show()
    sns.heatmap(X,cmap="YlGnBu")
    plt.show()
    sns.heatmap(X,cmap="YlGnBu_r")
    plt.show()
    

    0 讨论(0)
提交回复
热议问题