1
votes

I have the following strange behavior: When I limit the range of the figure, the colorplot shows it nevertheless:

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()

x = np.linspace(0,1,100)
X,Y = np.meshgrid(x,x,indexing="ij")

im = ax.contourf(X,Y,X**2-Y**2, 100, vmin = 0, vmax = 0.5)

plt.colorbar(im, ax=ax)

plt.show()

enter image description here

how can I configure the limits of the colorbar correctly?

1

1 Answers

5
votes

The 100 within the ax.contourf() means that you want 100 levels within the contour. You do have values that go over 0.5 within the plot itself.

You can customize the range of the color bar ticks as such.

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cbarticks = np.arange(0.0,0.55,0.05)
x = np.linspace(0,1,100)
X,Y = np.meshgrid(x,x,indexing="ij")

im = ax.contourf(X,Y,X**2-Y**2, cbarticks, vmin = 0, vmax = 0.5)

plt.colorbar(im, ax=ax,ticks=cbarticks)

plt.show()

which will give you

enter image description here

Unsure if this is exactly what you want but I had a similar question and answered it myself here: Colorbar Question