Jacques MALAPRADE - 4 months ago 45

Python Question

I have seen so many examples that just don't apply to my case. What I would like to do is set a simple minimum and maximum value for a colorbar. Setting a range for an image cmap is easy but this does not apply the same range to the minimum and maximum values of the colorbar.

The code below may explain:

`triang = Triangulation(x,y)`

plt.tricontourf(triang, z, vmax=1., vmin=0.)

plt.colorbar()

The colorbar is still fixed to the limits of the data z, although the cmap range is now fixed between 0 and 1.

Answer

I propose you incorporate you plot in a fig and get inspiration from this sample using the colorbar

```
data = np.tile(np.arange(4), 2)
fig = plt.figure()
ax = fig.add_subplot(121)
cax = fig.add_subplot(122)
cmap = colors.ListedColormap(['b','g','y','r'])
bounds=[0,1,2,3,4]
norm = colors.BoundaryNorm(bounds, cmap.N)
im=ax.imshow(data[None], aspect='auto',cmap=cmap, norm=norm)
cbar = fig.colorbar(im, cax=cax, cmap=cmap, norm=norm, boundaries=bounds,
ticks=[0.5,1.5,2.5,3.5],)
plt.show()
```

you see that you can set `bounds`

for the colors in colorbar and ticks.

it is not rigourously what you want to achieve, but the hint to fig could help.

This other one uses `ticks`

as well to define the scale of colorbar.

```
import numpy as np
import matplotlib.pyplot as plt
xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
[0., 1.0, 2.0],
[-0.1, 1.0, 2.0]])
v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()
```