Set Colorbar Range in matplotlib

后端 未结 4 549
心在旅途
心在旅途 2020-11-22 16:24

I have the following code:

import matplotlib.pyplot as plt

cdict = {
  \'red\'  :  ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
  \'green\':  ( (0.0         


        
4条回答
  •  隐瞒了意图╮
    2020-11-22 17:11

    Not sure if this is the most elegant solution (this is what I used), but you could scale your data to the range between 0 to 1 and then modify the colorbar:

    import matplotlib as mpl
    ...
    ax, _ = mpl.colorbar.make_axes(plt.gca(), shrink=0.5)
    cbar = mpl.colorbar.ColorbarBase(ax, cmap=cm,
                           norm=mpl.colors.Normalize(vmin=-0.5, vmax=1.5))
    cbar.set_clim(-2.0, 2.0)
    

    With the two different limits you can control the range and legend of the colorbar. In this example only the range between -0.5 to 1.5 is show in the bar, while the colormap covers -2 to 2 (so this could be your data range, which you record before the scaling).

    So instead of scaling the colormap you scale your data and fit the colorbar to that.

提交回复
热议问题