I\'m trying to plot an asymmetric color range in a scatter plot. I want the colors to be a fair representation of the intensity using a diverging color map. I am having trouble
If I get you correctly, the issue at hand is that your midpoint-centered map is scaling the color evenly from -2 to 0 (blue) and similarly (red) from 0 to 10.
Instead of scaling [self.vmin, self.midpoint, self.vmax] = [-2, 0, 10]
, you should rather rescale between [-v_ext, self.midpoint, v_ext] = [-10, 0, 10]
where:
v_ext = np.max( [ np.abs(self.vmin), np.abs(self.vmax) ] ) ## = np.max( [ 2, 10 ] )
The complete code could look like:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2
class MidpointNormalize(mcolors.Normalize):
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
self.midpoint = midpoint
mcolors.Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
v_ext = np.max( [ np.abs(self.vmin), np.abs(self.vmax) ] )
x, y = [-v_ext, self.midpoint, v_ext], [0, 0.5, 1]
return np.ma.masked_array(np.interp(value, x, y))
x = np.arange( 0, 1, 1e-1 )
xlen = x.shape[ 0 ]
z = np.random.random( xlen**2 )*12 - 2
norm = MidpointNormalize( midpoint = 0 )
splt = plt.scatter(
np.repeat( x, xlen ),
np.tile( x, xlen ),
c = z, cmap = 'seismic', s = 400,
norm = norm
)
plt.colorbar( splt )
plt.show()