Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Showing Levels end values on contourf

I’m using a contourf to plot my data (var) and I would like to have 20 levels going from -100 to 100, so this what I did.

plt.contourf(var, levels=np.linspace(-100, 100, 21))

But when I plot it it will miss the end values (-100 and 100), how can I solve it and show these values?

Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Image

>Solution :

You can have a fine control of the values shown in the colorbar:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.mgrid[-2:2:100j,-2:2:100j]
z = 100 * np.cos(x**2 + y**2)

fig, ax = plt.subplots()
c = ax.contourf(x, y, z, levels=np.linspace(-100, 100, 21))
cb = fig.colorbar(c)
ticks = np.linspace(-100, 100, 9)
# if you change ticks, you want to change the following as well
labels = [str(int(t)) for t in ticks]
cb.set_ticks(ticks, labels=labels)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading