PLOTS contourΒΆ

# -*-Python-*-
# Created by bgrierson at 15 Dec 2016  04:53

# Make some 2D data
nx = 101
nt = 51
x = linspace(0, 1, nx)
t = linspace(0, 1, nt)
tt, xx = meshgrid(t, x)
f = np.sin(2.0 * np.pi * (tt - 0.1 * (1.0 - xx)))

# Make a contour plot
fig, ax = plt.subplots()
ax.contour(t, x, f)

# Now make a contour plot with the contour and lines at
# the contour levels
fig, ax = plt.subplots()
ax.contourf(t, x, f)
ax.contour(t, x, f, colors='black')

# Now make a contour plot where we define the levels
v = linspace(-1, 1, 21)
fig, ax = plt.subplots()
ax.contourf(t, x, f, v)
ax.contour(t, x, f, v, colors='black')

# Now make a contour plot with a colorbar
fig, ax = plt.subplots()
con = ax.contourf(t, x, f)
conl = ax.contour(t, x, f, colors='black')
fig.subplots_adjust(right=0.8)
# Add a new axis starting at nomralized postion x=0.9
# and y=0.1 with normalized width 0.03 and height 0.8
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
# Add the colorbar
fig.colorbar(con, cax=cax)

# Now make a pair of contour plots where we put two contours on one page and zoom in on a subset of data
# and restrict the counter limits for the second contour
fig, ax = plt.subplots(nrows=2)
# Make the first contour plot
c0 = ax[0].contourf(t, x, f)
fig.subplots_adjust(right=0.8)
# Get the position of this contour
cpos = ax[0].get_position()
bot = cpos.p0[1]
top = cpos.p1[1]
# Add the axes object for the colorbar
cax = fig.add_axes([0.85, bot, 0.03, top - bot])
fig.colorbar(c0, cax=cax)

# Make the second contour plot.  Set either vmin, vmax or c1.clim()
cmin = 0.0
cmax = 1.0
c1 = ax[1].contourf(t, x, f)
c1.set_clim(cmin, cmax)
# Zoom in on one part of the plot
ax[1].set_xlim([0, 0.5])
ax[1].set_ylim([0, 0.5])
fig.subplots_adjust(right=0.8)
# Get the position of this contour
cpos = ax[1].get_position()
bot = cpos.p0[1]
top = cpos.p1[1]
# Add the axes object for the colorbar
cax = fig.add_axes([0.85, bot, 0.03, top - bot])
# Set the colorbar as its own object
m = plt.cm.ScalarMappable(cmap=plt.cm.viridis)
m.set_array(f)
m.set_clim(cmin, cmax)
cb = fig.colorbar(m, cax=cax)