Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib normalize colorbar (Python)

I'm trying to plot a contourf-plot using matplotlib (and numpy of course). And it works, it plots what it should plot, but unfortunatelly I cannot set the colorbar range. The problem is that I have a plenty of plots and need all of them to have the same colorbar (same min and max, same colors). I copy&past-ed almost every code snippet I found on the internet, but without success. My code so far:

    import numpy as np;
    import matplotlib as mpl;
    import matplotlib.pyplot as plt;
    [...]
    plotFreq, plotCoord = np.meshgrid(plotFreqVect, plotCoordVect);

    figHandler = plt.figure();
    cont_PSD = plt.contourf(plotFreq, plotCoord, plotPxx, 200, linestyle=None);


    normi = mpl.colors.Normalize(vmin=-80, vmax=20);

    colbar_PSD = plt.colorbar(cont_PSD);
    colbar_PSD.set_norm(normi);
    #colbar_PSD.norm = normi;
    #mpl.colors.Normalize(vmin=-80, vmax=20);

    plt.axis([1, 1000, -400, 400]);

As you can see there are three different lines for the colorbar norm, none of them is working. The range is still set automatically... I mean everything else is working, why not the colorbar? I don't even get errors or warnings.

Thanks, itpdg

EDIT 1: Pictures, with plt.clim(-80,20):

enter image description here

like image 452
itpdg Avatar asked Feb 22 '16 15:02

itpdg


People also ask

How do you normalize a Colorbar in Python?

will map the data in Z linearly from -1 to +1, so Z=0 will give a color at the center of the colormap RdBu_r (white in this case). Matplotlib does this mapping in two steps, with a normalization from the input data to [0, 1] occurring first, and then mapping onto the indices in the colormap.

What does MatPlotLib colors normalize do?

The matplotlib. colors. Normalize class is used to normalize data into the interval of [0.0, 1.0]. If either of vmin or vmax is not set then it initializes from the minimum and maximum value of the first input processed respectively.

How do you reverse a colormap in Python?

We can reverse the colormap of the plot with the help of two methods: By using the reversed() function to reverse the colormap. By using “_r” at the end of colormap name.


1 Answers

Please user the levels parameter, a set of examples:

In [9]:

ndom
z = np.random.random((10,10))

Without levels, colorbar will be auto-scaled

In [11]:
plt.contourf(z)
plt.colorbar()
Out[11]:
<matplotlib.colorbar.Colorbar at 0x120d47390>

enter image description here

In [12]:
plt.contourf(z*2)
plt.colorbar()
Out[12]:
<matplotlib.colorbar.Colorbar at 0x120f6ac10>

enter image description here

Control colorbar with explicit levels

In [13]:
plt.contourf(z*2, levels=np.linspace(0,2,20))
plt.colorbar()
Out[13]:
<matplotlib.colorbar.Colorbar at 0x121b119d0>

enter image description here

In [14]:
plt.contourf(z, levels=np.linspace(0,2,20))
plt.colorbar()
Out[14]:
<matplotlib.colorbar.Colorbar at 0x120dc3510>

enter image description here

like image 122
CT Zhu Avatar answered Sep 28 '22 07:09

CT Zhu