Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the limits on a colorbar of a contour plot

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.

like image 263
Jacques MALAPRADE Avatar asked Feb 22 '14 09:02

Jacques MALAPRADE


People also ask

How do you change the contour color in python?

The default color scheme of Matplotlib contour and filled contour plots can be modified. A general way to modify the color scheme is to call Matplotlib's plt. get_cmap() function that outputs a color map object. There are many different colormaps available to apply to contour plots.


2 Answers

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()
like image 167
kiriloff Avatar answered Sep 21 '22 12:09

kiriloff


This is the simplest method probably.

...(your code as shown)

plt.colorbar(boundaries=np.linspace(0,1,5)) 

...

like image 41
Yogesh Luthra Avatar answered Sep 21 '22 12:09

Yogesh Luthra