Meaning of cmap in contourf
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()
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
.
inquiries
Updated on June 07, 2022Comments
-
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.In the
contourf
function, there is a variable namedcmap
. What is this used for and what is its meaning? And what iscmap=cm.jet
mean?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 acontourf
handle?
-
inquiries about 8 yearsThank you for your lucid answer. Much appreciated.
-
inquiries about 8 yearsBut 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 about 8 years@user4437416 If you want to do that I would just use the range of the data itself to determine
min
andma
.min = data.flatten().min()
anddata.flatten().max()