|
| 1 | +""" |
| 2 | +======================================= |
| 3 | +Contour plot of irregularly spaced data |
| 4 | +======================================= |
| 5 | +
|
| 6 | +Comparison of a contour plot of irregularly spaced data interpolated |
| 7 | +on a regular grid versus a tricontour plot for an unstructured triangular grid. |
| 8 | +
|
| 9 | +Since :meth:`~.axes.Axes.contour` and :meth:`~.axes.Axes.contourf` expect the |
| 10 | +data to live on a regular grid, plotting a contour plot of irregularly spaced |
| 11 | +data requires different methods. The two options are: |
| 12 | +
|
| 13 | +* Interpolate the data to a regular grid first. This can be done with on-borad |
| 14 | + means, e.g. via `~.tri.LinearTriInterpolator` or using external functionality |
| 15 | + e.g. via `scipy.interpolate.griddata`. The latter is more versatile |
| 16 | + as it allows for different kinds of interpolation (e.g. cubic). Then plot the |
| 17 | + interpolated data with the usual :meth:`~.axes.Axes.contour`. |
| 18 | +* Directly use :meth:`~.axes.Axes.tricontour` or :meth:`~.axes.Axes.tricontourf` |
| 19 | + which will perform a triangulation internally. |
| 20 | +
|
| 21 | +This example shows both methods in action. |
| 22 | +""" |
| 23 | + |
| 24 | +import matplotlib.pyplot as plt |
| 25 | +import matplotlib.tri as tri |
| 26 | +import numpy as np |
| 27 | + |
| 28 | +np.random.seed(19680801) |
| 29 | +npts = 200 |
| 30 | +ngridx = 100 |
| 31 | +ngridy = 200 |
| 32 | +x = np.random.uniform(-2, 2, npts) |
| 33 | +y = np.random.uniform(-2, 2, npts) |
| 34 | +z = x * np.exp(-x**2 - y**2) |
| 35 | + |
| 36 | +fig, (ax1, ax2) = plt.subplots(nrows=2) |
| 37 | + |
| 38 | +# ----------------------- |
| 39 | +# Interpolation on a grid |
| 40 | +# ----------------------- |
| 41 | +# A contour plot of irregularly spaced data coordinates |
| 42 | +# via interpolation on a grid. |
| 43 | + |
| 44 | +# Create grid values first. |
| 45 | +xi = np.linspace(-2.1, 2.1, ngridx) |
| 46 | +yi = np.linspace(-2.1, 2.1, ngridy) |
| 47 | + |
| 48 | +# Perform linear interpolation of the data (x,y) |
| 49 | +# on a grid defined by (xi,yi) |
| 50 | +triang = tri.Triangulation(x, y) |
| 51 | +interpolator = tri.LinearTriInterpolator(triang, z) |
| 52 | +Xi, Yi = np.meshgrid(xi, yi) |
| 53 | +zi = interpolator(Xi, Yi) |
| 54 | + |
| 55 | +# Note that scipy.interpolate provides means to interpolate data on a grid |
| 56 | +# as well. The following would be an alternative to the four lines above: |
| 57 | +#from scipy.interpolate import griddata |
| 58 | +#zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear') |
| 59 | + |
| 60 | + |
| 61 | +ax1.contour(xi, yi, zi, 14, linewidths=0.5, colors='k') |
| 62 | +cntr1 = ax1.contourf(xi, yi, zi, 14, cmap="RdBu_r") |
| 63 | + |
| 64 | +fig.colorbar(cntr1, ax=ax1) |
| 65 | +ax1.plot(x, y, 'ko', ms=3) |
| 66 | +ax1.axis((-2, 2, -2, 2)) |
| 67 | +ax1.set_title('grid and contour (%d points, %d grid points)' % |
| 68 | + (npts, ngridx * ngridy)) |
| 69 | + |
| 70 | + |
| 71 | +# ---------- |
| 72 | +# Tricontour |
| 73 | +# ---------- |
| 74 | +# Directly supply the unordered, irregularly spaced coordinates |
| 75 | +# to tricontour. |
| 76 | + |
| 77 | +ax2.tricontour(x, y, z, 14, linewidths=0.5, colors='k') |
| 78 | +cntr2 = ax2.tricontourf(x, y, z, 14, cmap="RdBu_r") |
| 79 | + |
| 80 | +fig.colorbar(cntr2, ax=ax2) |
| 81 | +ax2.plot(x, y, 'ko', ms=3) |
| 82 | +ax2.axis((-2, 2, -2, 2)) |
| 83 | +ax2.set_title('tricontour (%d points)' % npts) |
| 84 | + |
| 85 | +plt.subplots_adjust(hspace=0.5) |
| 86 | +plt.show() |
| 87 | + |
| 88 | +############################################################################# |
| 89 | +# |
| 90 | +# ------------ |
| 91 | +# |
| 92 | +# References |
| 93 | +# """""""""" |
| 94 | +# |
| 95 | +# The use of the following functions and methods is shown in this example: |
| 96 | + |
| 97 | +import matplotlib |
| 98 | +matplotlib.axes.Axes.contour |
| 99 | +matplotlib.pyplot.contour |
| 100 | +matplotlib.axes.Axes.contourf |
| 101 | +matplotlib.pyplot.contourf |
| 102 | +matplotlib.axes.Axes.tricontour |
| 103 | +matplotlib.pyplot.tricontour |
| 104 | +matplotlib.axes.Axes.tricontourf |
| 105 | +matplotlib.pyplot.tricontourf |
0 commit comments