Plot Functions

Plot Functions

Standard Plots Using the Plot Recipe

Plotting functionality is provided by recipes to Plots.jl. To use plot solutions, simply call the plot(type) after importing Plots.jl and the plotter will generate appropriate plots.

#Pkg.add("Plots") # You need to install Plots.jl before your first time using it!
using Plots
plot(sol) # Plots the solution

Many of the types defined in the DiffEq universe, such as ODESolution, ConvergenceSimulation WorkPrecision, etc. have plot recipes to handle the default plotting behavior. Plots can be customized using all of the keyword arguments provided by Plots.jl. For example, we can change the plotting backend to the GR package and put a title on the plot by doing:

plot(sol,title="I Love DiffEqs!")


If the problem was solved with dense=true, then denseplot controls whether to use the dense function for generating the plot, and plotdensity is the number of evenly-spaced points (in time) to plot. For example:


means "only plot the points which the solver stepped to", while:


means to plot 1000 points using the dense function (since denseplot=true by default).

Choosing Variables

In the plot command, one can choose the variables to be plotted in each plot. The master form is:

vars = [(f1,0,1), (f2,1,3), (f3,4,5)]

which could be used to plot f1(var₀, var₁), f2(var₁, var₃), and f3(var₄, var₅), all on the same graph. (0 is considered to be time, or the independent variable). Functions f1, f2 and f3 should take in scalars and return a tuple. If no function is given, for example,

vars = [(0,1), (1,3), (4,5)]

this would mean "plot var₁(t) vs t (time), var₃(var₁) vs var₁, and var₅(var₄) vs var₄ all on the same graph, putting the independent variables (t, var₁ and var₄) on the x-axis." While this can be used for everything, the following conveniences are provided:

vars = [1, (1,3), (4,5)]


vars = [1, 3, 4]

is the most concise way to plot the variables 1, 3, and 4 as a function of time.

vars = ([1,2,3], [4,5,6])

is equivalent to

vars = [(1,4), (2,5), (3,6)]


vars = (1, [2,3,4])

is equivalent to

vars = [(1,2), (1,3), (1,4)]

Complex Numbers and High Dimensional Plots

The recipe library DimensionalPlotRecipes.jl is provided for extra functionality on high dimensional numbers (complex numbers) and other high dimensional plots. See the README for more details on the extra controls that exist.


using DifferentialEquations, Plots
lorenz = @ode_def Lorenz begin
  dx = σ*(y-x)
  dy = ρ*x-y-x*z
  dz = x*y-β*z
end σ = 10.0 β = 8.0/3.0 ρ => 28.0

u0 = [1., 5., 10.]
tspan = (0., 100.)
prob = ODEProblem(lorenz, u0, tspan)
sol = solve(prob)

xyzt = plot(sol, plotdensity=10000,lw=1.5)
xy = plot(sol, plotdensity=10000, vars=(:x,:y))
xz = plot(sol, plotdensity=10000, vars=(:x,:z))
yz = plot(sol, plotdensity=10000, vars=(:y,:z))
xyz = plot(sol, plotdensity=10000, vars=(:x,:y,:z))
plot(plot(xyzt,xyz),plot(xy, xz, yz, layout=(1,3),w=1), layout=(2,1))


An example using the functions:

f(x,y,z) = (sqrt(x^2+y^2+z^2),x)



Using the iterator interface over the solutions, animations can also be generated via the animate(sol) command. One can choose the filename to save to via animate(sol,filename), while the frames per second fps and the density of steps to show every can be specified via keyword arguments. The rest of the arguments will be directly passed to the plot recipe to be handled as normal. For example, we can animate our solution with a larger line-width which saves every 4th frame via:

#Pkg.add("ImageMagick") # You may need to install ImageMagick.jl before your first time using it!
#using ImageMagick # Some installations require using ImageMagick for good animations

Please see Plots.jl's documentation for more information on the available attributes.

Plotting Without the Plot Recipe

What if you don't want to use Plots.jl? Odd choice, but that's okay! If differential equation was described by a vector of values, then the solution object acts as an AbstractMatrix sol[i,j] for the ith variable at timepoint j. You can use this to plot solutions. For example, in PyPlot, Gadfly, GR, etc., you can do the following to plot the timeseries:


since these plot along the columns, and sol' has the timeseries along the column. Phase plots can be done similarly, for example:


is a 3d phase plot between variables i, j, and k.

Notice that this does not use the interpolation. When not using the plot recipe, the interpolation must be done manually. For example:

dt = 0.001 #spacing in time
ts = linspace(0,1,dt)

is the phase space using values 0.001 apart in time.