Reputation: 6854
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()
how can I configure the limits of the colorbar correctly?
Upvotes: 1
Views: 4634
Reputation: 332
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
Unsure if this is exactly what you want but I had a similar question and answered it myself here: Colorbar Question
Upvotes: 5