matplotlib - How to create a heat map in python that ranges from green to red? -
i'm trying plot log ratios range -3 3 , want negative ratios green , positive red, log ratio of 0 (center) white in color. none of pre-existing color schemes in matplotlib provide option, , haven't been able figure out how output nice gradient manually.
you can create own using linearsegmentedcolormap
. set red , green channels less 1.0 @ upper , lower limits colours aren't bright (here used 0.8). adjust suit taste.
see custom_cmap example on matplotlib website further details.
here's working example:
import matplotlib.pyplot plt import matplotlib.colors colors import numpy np # dictionary defines colormap cdict = {'red': ((0.0, 0.0, 0.0), # no red @ 0 (0.5, 1.0, 1.0), # channels set 1.0 @ 0.5 create white (1.0, 0.8, 0.8)), # set 0.8 not bright @ 1 'green': ((0.0, 0.8, 0.8), # set 0.8 not bright @ 0 (0.5, 1.0, 1.0), # channels set 1.0 @ 0.5 create white (1.0, 0.0, 0.0)), # no green @ 1 'blue': ((0.0, 0.0, 0.0), # no blue @ 0 (0.5, 1.0, 1.0), # channels set 1.0 @ 0.5 create white (1.0, 0.0, 0.0)) # no blue @ 1 } # create colormap using dictionary gnrd = colors.linearsegmentedcolormap('gnrd', cdict) # make figure , axes fig,ax = plt.subplots(1) # fake data in range -3 3 dummydata = np.random.rand(5,5)*6.-3. # plot fake data p=ax.pcolormesh(dummydata,cmap=gnrd,vmin=-3,vmax=3) # make colorbar fig.colorbar(p,ax=ax) plt.show()
Comments
Post a Comment