In python, If I have a set of data
x, y, z
I can make a scatter with
import matplotlib.pyplot as plt
plt.scatter(x,y,c=z)
How I can get a plt.contourf(x,y,z)
of the scatter ?
In python, If I have a set of data
x, y, z
I can make a scatter with
import matplotlib.pyplot as plt
plt.scatter(x,y,c=z)
How I can get a plt.contourf(x,y,z)
of the scatter ?
contour
expects regularly gridded data. You thus need to interpolate your data first:Note that I shamelessly stole this code from the excellent matplotlib cookbook
Use the following function to convert to the format required by contourf:
Now you can do:
The solution will depend on how the data is organized.
Data on regular grid
If the
x
andy
data already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.can plotted as a
contour
usingArbitrary data
a. Interpolation
In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid.
One method to do so is provided by matplotlib itself, usingThis method is deprecated. Alternative: usingmatplotlib.mlab.griddata
.scipy.interpolate.griddata
b. Non-gridded contour
Finally, one can plot a contour completely without the use of a quadrilateral grid. This can be done using
tricontour
.An example comparing the latter two methods is found on the matplotlib page.