varantir
varantir

Reputation: 6854

colorbar does not apply vmin and vmax

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?

Upvotes: 1

Views: 4634

Answers (1)

twseewx
twseewx

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

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

Upvotes: 5

Related Questions