Reverse colormap in matplotlib

前端 未结 7 805
小蘑菇
小蘑菇 2020-11-28 02:04

I would like to know how to simply reverse the color order of a given colormap in order to use it with plot_surface.

7条回答
  •  佛祖请我去吃肉
    2020-11-28 02:58

    There is no built-in way (yet) of reversing arbitrary colormaps, but one simple solution is to actually not modify the colorbar but to create an inverting Normalize object:

    from matplotlib.colors import Normalize
    
    class InvertedNormalize(Normalize):
        def __call__(self, *args, **kwargs):
            return 1 - super(InvertedNormalize, self).__call__(*args, **kwargs)
    

    You can then use this with plot_surface and other Matplotlib plotting functions by doing e.g.

    inverted_norm = InvertedNormalize(vmin=10, vmax=100)
    ax.plot_surface(..., cmap=, norm=inverted_norm)
    

    This will work with any Matplotlib colormap.

提交回复
热议问题