bernski
bernski

Reputation: 33

Customizing the limits on the matplotlib colorbar

I have a dataset which plots concentration as the z-value. Note the oddball limits in the colorbar which are mapped to the limits of the z-value. What I want is to have the colorbar limits go from 0 to some max value (logically the first rounded integer beyond z_max).

plot of concentrations

Here is the code snippet:

from numpy import *
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
.
.
.

monthly_concentration_array=master_yearly_df.to_numpy()
year_axis_values=master_yearly_df.index.to_numpy()
month_numbers=arange(12)

fig,ax=plt.subplots(figsize=(10,15))

contour_plot=ax.contourf(month_numbers,
    year_axis_values,monthly_concentration_array,levels=40,cmap='rainbow')
colour_bar=fig.colorbar(contour_plot)

I naively used

colour_bar.set_clim(0,5)

But I received a deprecation warning:

MatplotlibDeprecationWarning: The set_clim function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use ScalarMappable.set_clim instead.

One other post suggested doing

colour_bar.mappable.set_clim(0,5)

But that did not work. I am very confused because the vast majority of posts etc predate matplotlib 3.1 and sadly I don't have enough under-the-hood experience to understand what the reference "Use ScalarMappable.set_clim instead" means.

So what is the correct, current way to set colorbar limits so that the scale goes from 0 to 5 when the data only goes from 0.15 to 4.7?

many thanks!

Upvotes: 1

Views: 2089

Answers (1)

Smectic
Smectic

Reputation: 114

You can add the option vmin=0, vmax=5 in ax.contourf(), it will override the default color scaling.

Upvotes: 2

Related Questions