I'm wondering how could I save the data content of a plot generated using Matplotlib to a Numpy array.
As a example, suppose I generated a contour plot with the following code:
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS = plt.contour(X, Y, Z)
plt.show()
From that I get the following:
I'm wondering how I could save this data so, after some other manipulations, if I show that data with imshow
, for example, I could recover the contours color mapped and filled like the original image.
EDIT:
I.e., I would like to be able to get the image generate by the contourf
method, do some manipulation to it, like to apply some mask in specific areas, and then plot this modified data. For the contour
case, I would like to use this plot with a bunch of levels represented to do the manipulations, instead of iterating over the cs.collection
and do something (which I really don't know what) to obtain an equivalent numpy.array
to represent this plot.
I know that I can save the image to a file than read this file but that seems a poor solution to me. I tried that solution too, but then I got the full plot, with green areas and so on, not just the real content.