Custom Colormap

前端 未结 1 428
名媛妹妹
名媛妹妹 2021-01-13 13:09

I want to plot a heatmap with a custom colormap similar to this one, although not exactly.

I\'d like to have a colormap that goes like this

相关标签:
1条回答
  • 2021-01-13 13:20

    Colormaps are normalized in the 0..1 range. So if your data limits are -1..1, -0.6 would be normalized to 0.2, +0.6 would be normalized to 0.8.

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.colors
    
    norm = matplotlib.colors.Normalize(-1,1)
    colors = [[norm(-1.0), "darkblue"],
              [norm(-0.6), "lightgrey"],
              [norm( 0.6), "lightgrey"],
              [norm( 1.0), "red"]]
    
    cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
    
    
    fig, ax=plt.subplots()
    x = np.arange(10)
    y = np.linspace(-1,1,10)
    sc = ax.scatter(x,y, c=y, norm=norm, cmap=cmap)
    fig.colorbar(sc, orientation="horizontal")
    plt.show()
    

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