Colorplot that distinguishes between positive and negative values

前端 未结 2 1845
鱼传尺愫
鱼传尺愫 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 09:43

    there is a lot of good example material on simple self-defined segmented color bars on the matplotlib documentation pages

    for instance

    http://matplotlib.org/examples/api/colorbar_only.html http://matplotlib.org/examples/pylab_examples/contourf_demo.html

    EDIT:

    from what I understand, this might be the perfect example for what you are looking for:

    http://matplotlib.org/examples/pylab_examples/custom_cmap.html

    0 讨论(0)
  • 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

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