Home > Net >  How to set global scale for colors in pcolormesh?
How to set global scale for colors in pcolormesh?

Time:10-21

I have different datasets to plot using pcolormesh, I generate images like this for every data.

For every image, the scale changes as the normalization sets minimum and maximum values between 0 and 1. I need to set a global scale for colors, for example if 4 is equal to yellow in the first image, it will be the same color in every image. So the values for every color are general.

This is part of the code I'm using to plot the images.

f, t, Zxx = signal.stft(inputSignal, samplingFreq, window=window, nperseg=nperseg)
##Plotting STFT
fig = plt.figure(figsize=figsize, dpi = 100)
spec = plt.pcolormesh(t, f, Zxx, norm=colors.PowerNorm(gamma=1./8.),cmap=plt.get_cmap(cmap))

STFT

CodePudding user response:

Without any example code/output it's a little difficult to understand what you mean. But you can set the vmin and vmax keywords on the norm that you use. If you omit that, Matplotlib will derive them from the data used, and they will indeed change if the data changes. This is true for other normalizations as well, not just the PowerNorm.

norm = mpl.colors.PowerNorm(gamma=1./8., vmin=0, vmax=2)

If you need to use the same properties for many you could put them in a dict once, up front. And unpack that whenever you call the plotting method, like:

style = {
    "cmap": cmap, # mpl.colormaps["Blues"].copy() ...
    "norm": mpl.colors.PowerNorm(gamma=1./8., vmin=0, vmax=2),
}

ax.pcolormesh(t, f, Zxx, **style)

https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.PowerNorm.html#matplotlib.colors.PowerNorm

  • Related