0

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

enter image description here

Scrabyard
  • 155
  • 1
  • 2
  • 10
  • You might want to read more about [colormap normalization](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html). Which exactly you need with which parameters strongly depends on what you want to visualize and put emphasis on. For example `norm=plt.Normalize(vmin=mean - 2 * StdDev, vmax = mean +2 * StdDev)`. – JohanC May 01 '23 at 10:15
  • I found this entry, but this is not exactly what I am looking for, since it cuts off the Colorbar at the side at vmin and vmax. So I would like to keep the whole range at the right and just manipulate the gradient. – Scrabyard May 01 '23 at 10:30
  • Could you add some reproducible dummy test data to your post? And describe the desired colorbar for that example? – JohanC May 01 '23 at 10:34
  • Maybe this? https://stackoverflow.com/questions/65442636/change-colorbar-scaling-in-matplotlib – Joao_PS May 01 '23 at 19:43

0 Answers0