Plotting Using matplotlib

The matplotlib module provides a simple and easy to use way to visualize PDE solutions (or other Data objects). To hand over data from esys.escript to matplotlib the values need to mapped onto a rectangular grid [*]. We will make use of the numpy module.

First we need to create a rectangular grid. We use the following statements:
\begin{python}
import numpy
x_grid = numpy.linspace(0.,1.,50)
y_grid = numpy.linspace(0.,1.,50)
\end{python}
x_grid is an array defining the x coordinates of the grids while y_grid defines the y coordinates of the grid. In this case we use $ 50$ points over the interval $ [0,1]$ in both directions.

Now the values created by esys.escript need to be interpolated to this grid. We will use the matplotlib mlab.griddata function to do this. We can easily extract spatial coordinates as a list by
\begin{python}
x=mydomain.getX()[0].toListOfTuples()
y=mydomain.getX()[1].toListOfTuples()
\end{python}
In principle we can apply the same toListOfTuples method to extract the values from the PDE solution u. However, we have to make sure that the Data object we extract the values from uses the same FunctionSpace as we have us when extracting x and y. We apply the interpolation to u before extraction to achieve this:
\begin{python}
z=interpolate(u,mydomain.getX().getFunctionSpace())
\end{python}
The values in z are now the values at the points with the coordinates given by x and y. These values are now interpolated to the grid defined by x_grid and y_grid by using
\begin{python}
import matplotlib
z_grid = matplotlib.mlab.griddata(x,y,z,xi=x_grid,yi=y_grid )
\end{python}
z_grid gives now the values of the PDE solution u at the grid. The values can be plotted now using the contourf:
\begin{python}
matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
matplotlib.pyplot.savefig(''u.png'')
\end{python}
Here we use $ 5$ contours. The last statement writes the plot to the file u.png in the PNG format. Alternatively, one can use
\begin{python}
matplotlib.pyplot.contourf(x_grid, y_grid, z_grid, 5)
matplotlib.pyplot.show()
\end{python}
which gives an interactive browser window.

Figure: Visualization of the Poisson Equation Solution for $ f=1$ using matplotlib.
\includegraphics[width=100mm]{figures/FirstStepResultMATPLOTLIB}

Now we can write the script to solve our Poisson problem
\begin{python}
from esys.escript import *
from esys.escript.linearPDEs import Po...
...urf(x_grid, y_grid, z_grid, 5)
matplotlib.pyplot.savefig(''u.png'')
\end{python}
The entire code is available as poissonmatplotlib.py in the example directory. You can run the script using the escript environment

  escript poisson_matplotlib.py
This will create the u.png, see Figure Figure 1.3. For details on the usage of the matplotlib module we refer to the documentation [12].

As pointed out, matplotlib is restricted to the two-dimensional case and should be used for small problems only. It can not be used under MPI as the toListOfTuples method is not safe under MPI [*].

Figure 1.4: Visualization of the Poisson Equation Solution for $ f=1$
\includegraphics[width=100mm]{figures/FirstStepResult}

esys@esscc.uq.edu.au