# Plotting `solution.plot()` draws a solution with matplotlib, and `solution.animate()` shows how it changes in time. Both decide by themselves what a good picture of the problem is, and both leave every decision open to you. ```python import matplotlib.pyplot as plt import numpy as np import sympy as sp import pdemagic as pm x, y, z, t = sp.symbols("x y z t", real=True) ``` Plotting needs matplotlib. pdemagic itself does not, and imports it only when a plot is asked for. ## What is drawn | Problem | Picture | |---|---| | One dimension | A line. | | One dimension, in time | One line for each of five times, from light to dark. | | Two dimensions | The field in colour. | | Two dimensions, in time | The field at the last recorded time. | | Three dimensions | A section through the field, by default at the middle of the box along the last coordinate. | | A disk or a ring | The field in colour on it. | | A ball or a shell | A section through the axis: the plane at the angle zero, with its half across the axis. | | A cylinder | A section across the axis, at its middle. | | A shape in the plane | The field in colour, cut along the boundary. | A field with both signs is drawn in two colours around zero, any other in one. Two charges of opposite sign in the plane: ```python phi = sp.Function("phi")(x, y) charges = sp.exp(-((x - 1) ** 2 + y**2)) - sp.exp(-((x + 1) ** 2 + y**2)) dipole = pm.solve( pm.Problem(sp.Eq(pm.laplacian(phi), -charges), domain=pm.FreeSpace(x, y)) ) dipole.plot() ``` ## Times, and films For a [problem in time](time.md), `time=` chooses the time that is drawn. The recorded time nearest to it is taken. In one dimension it can be a list: ```python s = sp.Function("s")(x, t) string = pm.Problem( sp.Eq(sp.Derivative(s, t, 2), 9 * sp.Derivative(s, x, 2)), domain=pm.FreeSpace(x), time=(t, 0.0, 1.0), initial={s: sp.exp(-(x**2)), sp.Derivative(s, t): 0}, ) plucked = pm.solve(string) plucked.plot(time=[0.0, 0.5, 1.0]) ``` `animate` returns a matplotlib animation with one frame per recorded time: ```python film = plucked.animate() ``` `film.save("string.gif")` writes it to a file, and in a notebook `film.to_jshtml()` shows it. The number of frames is the number of recorded times, which is an entry of the [plan](planner.md): `plan.times.count`. The eigenfunctions of an [eigenproblem](eigen.md) are chosen by `state=` as the times are by `time=`: `states.plot(state=2)`. In one dimension a plot draws the lowest states together, up to five of them, and `states.plot(state=[0, 4])` chooses others. `animate` leafs through the states. ## Sections in three dimensions `cut` names the coordinate that is held fixed, and its value: ```python V = sp.Function("V")(x, y, z) cloud = pm.Problem( sp.Eq(pm.laplacian(V), -sp.exp(-(x**2 + y**2 + z**2))), domain=pm.FreeSpace(x, y, z), ) plan = pm.plan(cloud) plan.grid.shape = (32, 32, 32) potential = pm.solve(cloud, plan) potential.plot(cut={x: 1.0}) ``` In a ball and in a cylinder the coordinate that is held fixed decides what the section is. The angle around the axis gives the plane through the axis, with both of its halves. A ball that is heated on one side and cooled on the other: ```python r, theta, around = sp.symbols("r theta phi", real=True) W = sp.Function("W")(r, theta, around) ball = pm.Problem( sp.Eq(pm.laplacian(W), -r * sp.sin(theta) * sp.cos(around)), domain=pm.Ball(r, theta, around), boundary=sp.Eq(W, 0), ) warmed = pm.solve(ball) warmed.plot(cut={around: 0}) ``` The angle from the axis gives a cone, which at $\pi/2$ is the plane of the equator, drawn as a disk, and the radius gives a sphere, drawn by its two angles: `warmed.plot(cut={theta: np.pi / 2})`. In a cylinder the coordinate along the axis gives a disk, the angle the plane through the axis, and the radius a tube that is unrolled. ## Complex unknowns, and several Of a [complex unknown](complex.md) the magnitude is drawn. `part="real"` and `part="imag"` draw its two parts instead: ```python w = sp.Function("w")(x, y) source = sp.exp(-8 * (x**2 + y**2)) waves = pm.solve( pm.Problem(sp.Eq(pm.laplacian(w) + 36 * w, -source), domain=pm.FreeSpace(x, y)) ) waves.plot(part="real") ``` A solution of a [system](systems.md) holds several unknowns. Name the one to draw: `solution.plot(E)`, or `solution.plot("E")`. Without a name the first one is drawn. ## Your own figure `plot` returns the matplotlib axes that it drew into, so that everything about the figure can still be changed. `ax=` draws into axes of your own, which is how several plots go into one figure: ```python figure, (left, right) = plt.subplots(1, 2, figsize=(10, 4), layout="constrained") waves.plot(part="real", ax=left) waves.plot(ax=right) left.set_title("The wave at one instant", loc="left") right.set_title("Its amplitude", loc="left") ``` `theme="dark"` draws for a dark background. For anything `plot` does not do, take the arrays: `solution[u]` holds the values, `solution.axes` the grid points along each axis, and `solution.mesh()` the coordinates of every point. On a shape in the plane the points of the grid outside the shape hold `nan`. [Solutions](solutions.md) has more about them. ## See also - [Solutions](solutions.md), for the arrays behind a picture and for files that ParaView opens. - Reference: [Solution.plot and Solution.animate](../ref/solution.md). - The [gallery](../gallery/index.md), where every picture stands below the code that drew it.