I do many scatterplots (x vs. y) with a colormap to visualize a third variable (z). Since I do this for many z-variables, this has to work without manual intervention. Now the problem is, that some z-variables have only some very high values, so that the scaling of the colormap ist not useful (see figure).
How can I scale the colormap without scaling the z-values and still showing all datapoints? So the overall colormap should cover the whole range of z, but the actual color gradient should only span the meaningful range, e. g. -2 * StdDev to +2 * StdDev. So the gradient should be kind of centered, depending on the data distribution.
Here is my code so far:
def linreg(x, y, z, alpha=0.5, size=15):
figsize = [size, 0.75*size]
mpl.rcParams["font.size"] = 1.5625*size
a, b = np.polyfit(x, y, 1)
plt.figure(figsize=figsize)
plt.scatter(x=x, y=y, c=z, marker="o", alpha=alpha, cmap="viridis")
plt.colorbar(label=z.name)
plt.xlabel(x.name)
plt.ylabel(y.name)
plt.plot(x, a*x+b, color="k")
return(a, b, Rsq(y, a*x+b))