Colorplot that distinguishes between positive and negative values

前端 未结 2 1847
鱼传尺愫
鱼传尺愫 2021-01-12 09:25

As one can see in this sample code since 0 is somewhere in the spectrum it is hard to trace which points are negative and which are positive. Although my real plot is more c

2条回答
  •  逝去的感伤
    2021-01-12 10:05

    Ok for the future reference. I used diverging maps as part of it as @tcaswell suggested. You can look to the above links.

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.colors import BoundaryNorm
    a=np.random.randn(2500).reshape((50,50))
    
    # define the colormap
    cmap = plt.get_cmap('PuOr')
    
    # extract all colors from the .jet map
    cmaplist = [cmap(i) for i in range(cmap.N)]
    # create the new map
    cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
    
    # define the bins and normalize and forcing 0 to be part of the colorbar!
    bounds = np.arange(np.min(a),np.max(a),.5)
    idx=np.searchsorted(bounds,0)
    bounds=np.insert(bounds,idx,0)
    norm = BoundaryNorm(bounds, cmap.N)
    
    plt.imshow(a,interpolation='none',norm=norm,cmap=cmap)
    plt.colorbar()
    plt.show()
    

    enter image description here

提交回复
热议问题