1

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!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. It turns out that not asking the EXACT question in Google doesn't help. Changing the wording led me to this: stackoverflow.com/questions/43150687/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.