Np.histogram2D with fixed color gradient

I am trying to modify an existing bit of python code that displays a heatmap of values ​​with np.histogram2d. I draw several of them, and I want the y axis and color range to be comparable between them. I learned how to manually install y_limit, but now I would like the color range to also be fixed. The following is a snippet of code:

    hist,xedges,yedges = np.histogram2d(x,y, bins=[20, 50], range=[ [0, 100.0], [0, y_limit] ])
    # draw the plot
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1] ]
    im = ax.imshow(hist.T,extent=extent,interpolation='nearest',origin='lower', aspect='auto')
    # colormap, colorbar, labels, ect.
    im.set_cmap('gist_heat_r')
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "5%", pad="3%")
    ax.figure.colorbar(im, cax=cax)
    ax.set_title(d['name'] + ' foo')
    ax.set_xlabel("bar")
    ax.set_ylabel("coverage")

And an example showing different color ranges, where one goes from 0 to above 2800 and the other from 0 to 2400. I would like to be able to set the color range to a fixed maximum, for example. 3000 for both. Any ideas?

enter image description hereenter image description here

EDITED: Permitted with vminand vmax.

+4
source share
1 answer

,

class MidpointNormalize(Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
    self.midpoint = midpoint
    Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
    # I'm ignoring masked values and all kinds of edge cases to make a
    # simple example...
    x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
    return np.ma.masked_array(np.interp(value, x, y))

:

norm = MidpointNormalize(midpoint=0.5,vmin = 0, vmax = 1)
ax.scatter(x, y,c=z, cmap = cm, norm = norm)

imshow norm, .

+1

All Articles