4

I am trying to modify an existing bit of python code that plots a heatmap of values using np.histogram2d. I am plotting several of these and I want the y-axis and the colour range to be comparable among them. I found out the way to manually set y_limit, but now I would like the colour range to also be fixed. Code snippet below:

    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 the different colour ranges, where one goes from 0 to above 2800 and the other goes from 0 to above 2400. I would like to be able to set the colour range to a fixed maximum, e.g. 3000 for both. Any ideas?

enter image description here enter image description here

EDITED: resolved by using vmin and vmax.

719016
  • 9,922
  • 20
  • 85
  • 158

1 Answers1

1

I took a code snipped from this post

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))

I use it like:

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

imshow has the paramter norm too and it should work.

Community
  • 1
  • 1
Moritz
  • 5,130
  • 10
  • 40
  • 81