Question

I'm plotting several contour plots side by side for visualizing the time evolution of certain function. I want each contour's value and color to be shared between all subplots, but each time I add a new subplot, the contour values are recomputed (as shown in the image below), so any comparison between them is meaningless.

Contour plots with colorbars http://www.inf.utfsm.cl/~rbonvall/colorbar-and-contour-plots.png

I've tried setting manually different combinations of cmap, colorbar and axes attributes on each subplot instance, without success. How can I share the contour plot attributes between all the subplots? In other words, how to get the same colorbar for all subplots?

Was it helpful?

Solution

You can directly specify the contour values to be used in the contour plot. Here's an example:

alt text

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-1.2, 1.2, .025)
y = np.arange(-1.2, 1.2, .025)
X, Y = np.meshgrid(x, y)
Z = np.cos(X)*np.cos(Y)
Z = Z*Z

plt.subplot(1,2,1)
CS = plt.contour(X, Y, Z)   # set levels automatically
plt.clabel(CS, inline=1, fontsize=10)
plt.subplot(1,2,2)
CS = plt.contour(X, Y, Z-.1, CS.levels)  # set levels as previous levels
plt.clabel(CS, inline=1, fontsize=10)
plt.show()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top