3

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?

Suever
  • 64,497
  • 14
  • 82
  • 101
inquiries
  • 385
  • 2
  • 7
  • 20
  • `cmap` is a color map, try `cm.gray` for comparison. The colorbar should use the min/max of your plot. If I'm misunderstanding your question, why not post an example? – roadrunner66 Mar 09 '16 at 01:54

1 Answers1

6

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.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thank you for your lucid answer. Much appreciated. – inquiries Mar 09 '16 at 02:57
  • 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. – inquiries Mar 09 '16 at 11:42
  • 1
    @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()` – Suever Mar 09 '16 at 11:56