Instantly share code, notes, and snippets.

Embed
What would you like to do?
Normalize color by center and plot density plot in XoY way
from matplotlib.colors import Normalize
class CenterNorm(Normalize):
'''Demos see peijun.me'''
def __init__(self, vc=0, cc=0.5, vmin=None, vmax=None, clip=False):
'''
Args:
vc value of center
cc color of center
'''
Normalize.__init__(self, vmin, vmax, clip)
assert 0< cc < 1, "Central color should be in (0, 1)"
self.vc = vc
self.cc = cc
def __call__(self, value, clip=None):
dv = np.array([self.vc - self.vmin, self.vmax - self.vc])
dc = np.array([self.cc, 1 - self.cc])
k = 1/max(dv/dc)
return np.ma.masked_array((value-self.vc)*k+self.cc)
import numpy as np
def imshow_xy(ax, x, y, z, *args, **argv):
'''x is x of xoy rhs system, i.e. j index of image
y is y of xoy rhs system, i.e. -i index of image
z is function of x, y, i.e. z[i, j]=f(x[i], y[j])
Image is actual image matrix, I[i, j]=z[-j, i]'''
assert z.shape == (len(x), len(y)), "Shape unmatched"
ex = np.mean(np.diff(x))/2
ey = np.mean(np.diff(y))/2
argv['extent'] = (x[0]-ex, x[-1]+ex, y[0]-ey, y[-1]+ey)
return ax.imshow((z.T)[::-1, :], *args, **argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment