Meaning of cmap in contourf

13,717

The cmap kwarg is the colormap that should be used to display the contour plot. If you do not specify one, the jet colormap (cm.jet) is used. You can change this to any other colormap that you want though (i.e. cm.gray). matplotlib has a large number of colormaps to choose from.

Here is a quick demo showing two contour plots with different colormaps selected.

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

data = np.random.rand(10,10)

plt.subplot(1,2,1)
con = plt.contourf(data, cmap=cm.jet)
plt.title('Jet')
plt.colorbar()

hax = plt.subplot(1,2,2)
con = plt.contourf(data, cmap=cm.gray)
plt.title('Gray')
plt.colorbar()

enter image description here

As far as getting the upper/lower bounds on the colorbar programmatically, you can do this by getting the clim value of the contourf plot object.

con = plt.contourf(data);
limits = con.get_clim()

   (0.00, 1.05)

This returns a tuple containing the (lower, upper) bounds of the colorbar.

Share:
13,717
inquiries
Author by

inquiries

Updated on June 07, 2022

Comments

  • inquiries
    inquiries almost 2 years

    I have two questions regarding usage of the contourf plotting function. I have been searching for answers but haven't found them.

    1. In the contourf function, there is a variable named cmap. What is this used for and what is its meaning? And what is cmap=cm.jet mean?

    2. When one puts x,y,z into contourf and then creates a colorbar, how do we get the minimum and maximum values by which to set the colorbar limits? I am doing it manually now, but is there no way to get the min and max directly from a contourf handle?

  • inquiries
    inquiries about 8 years
    Thank you for your lucid answer. Much appreciated.
  • inquiries
    inquiries about 8 years
    But is there also any easy way to figure out the min and max of the z data being put into contourf? Instead of first plotting it corasely to get the max and min, it would be helpful if there were a automatic way from which i can then construct levs=np.linspace(min,ma,10000) to pu into the contourf levels argument.
  • Suever
    Suever about 8 years
    @user4437416 If you want to do that I would just use the range of the data itself to determine min and ma. min = data.flatten().min() and data.flatten().max()