Skip to content

Commit 4f81951

Browse files
authored
Merge pull request #819 from SciML/simulation_plotting_doc
Simulation plotting doc
2 parents edba70e + 0e26d74 commit 4f81951

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

docs/pages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pages = Any[
2626
],
2727
"Model simulation" => Any[
2828
# Simulation introduction.
29-
# Simulation plotting.
29+
"model_simulation/simulation_plotting.md",
3030
"model_simulation/simulation_structure_interfacing.md",
3131
"model_simulation/ensemble_simulations.md",
3232
# Stochastic simulation statistical analysis.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [Simulation_plotting](@id simulation_plotting)
2+
Catalyst uses the [Plots.jl](https://github.com/JuliaPlots/Plots.jl) package for performing all plots. This section provides a brief summary of some useful plotting options, while [Plots.jl's documentation](https://docs.juliaplots.org/stable/) provides a more throughout description of how to tune your plots.
3+
4+
!!! note
5+
[Makie.jl](https://github.com/MakieOrg/Makie.jl) is a popular alternative to the Plots.jl package. While it is not used within Catalyst's documentation, it is worth considering (especially for users interested in interactivity, or increased control over their plots).
6+
7+
## [Common plotting options](@id simulation_plotting_options)
8+
Let us consider the oscillating [Brusselator](@ref ref) model. We have previously shown how model simulation solutions can be plotted using the `plot` function. Here we plot an ODE simulation from the [Brusselator](@ref ref) model:
9+
```@example simulation_plotting
10+
using Catalyst, OrdinaryDiffEq, Plots
11+
12+
brusselator = @reaction_network begin
13+
A, ∅ --> X
14+
1, 2X + Y --> 3X
15+
B, X --> Y
16+
1, X --> ∅
17+
end
18+
u0 = [:X => 1.0, :Y => 0.0]
19+
tspan = (0.0, 50.0)
20+
ps = [:A => 1.0, :B => 4.0]
21+
22+
oprob = ODEProblem(brusselator, u0, tspan, ps)
23+
sol = solve(oprob)
24+
plot(sol)
25+
```
26+
27+
Various plotting options can be provided as optional arguments to the `plot` command. Common options include:
28+
- `lw`: Determine plot line widths.
29+
- `la`: Determine plot line's transparency (at `la = 0.0` lines are fully transparent, i.e. not visible).
30+
- `linestyle`: Determines plot line style.
31+
- `color`: Determines the line colours.
32+
- `legend`: Determines the position of the legend/labels.
33+
- `label`: Determines label texts.
34+
- `xguide`, `yguide`: Determines x and y axis labels.
35+
- `title`: Determines plot title.
36+
- `legendfontsize`, `guidefontsize`, `titlefontsize`: Determines the font size of the labels, x and y guides, and title, respectively.
37+
38+
Here, we re-plot our simulations, utilising some of these options (`legend = :none` is used to disable the legends).
39+
```@example simulation_plotting
40+
plot(sol; lw = 4, linestyle = :dash, color = :green, xguide = "Time", yguide = "Concentration", guidefontsize = 14)
41+
```
42+
Note that, by default, Catalyst uses `xguide = "t"`. Here, however, we modify this to `xguide = "Time"`. We also note that the `color = :green` change both lines' colours to green. To set different colours for each line, we provide these as *a vector without `,` in-between elements* (in Julia interpreted as a matrix with its first dimension equal to `1`):
43+
```@example simulation_plotting
44+
plot(sol; lw = 4, color = [:green :purple])
45+
```
46+
A full list of available colours can be found [here](https://juliagraphics.github.io/Colors.jl/stable/namedcolors/). A full list of possible plotting options can be found [here](https://docs.juliaplots.org/stable/attributes/) (look at the list of various plot attributes, e.g. "Series Attributes"). if there is some option(s) you intend to use multiple times, you can call the `default` function using these, in which case they will be used for all subsequent plots. E.g. here:
47+
```@example simulation_plotting
48+
default(framestyle = :box, grid = false)
49+
```
50+
we designate a box-style frame, and remove the faint background grid, for all subsequent plots in this tutorial.
51+
52+
A useful option unique to Catalyst (and other DifferentialEquations.jl-based) plots is `idxs`. Its input is a vector, listing all the species (or quantities) that should be plotted. I.e.
53+
```@example simulation_plotting
54+
plot(sol; idxs = [:X])
55+
```
56+
can be used to plot `X` only. When only a single argument is given, the vector form is unnecessary (e.g. `idxs = :X` could have been used instead). If [symbolic species representation is used](@ref ref), this can be used to designate any algebraic expression(s) that should be plotted. E.g. here we plot the total concentration of $X + Y$ throughout the simulation:
57+
```@example simulation_plotting
58+
plot(sol; idxs = brusselator.X + brusselator.Y)
59+
```
60+
61+
## [Multi-plot plots](@id simulation_plotting_options)
62+
It is possible to save plots in variables. These can then be used as input to the `plot` command. Here, the plot command can be used to create plots containing multiple plots (by providing multiple inputs). E.g. here we plot the concentration of `X` and `Y` in separate subplots:
63+
```@example simulation_plotting
64+
plt_X = plot(sol; idxs = [:X])
65+
plt_Y = plot(sol; idxs = [:Y])
66+
plot(plt_X, plt_Y)
67+
```
68+
69+
When working with subplots, the [`layout`](https://docs.juliaplots.org/latest/layouts/) and [`size`](https://docs.juliaplots.org/latest/generated/attributes_plot/) options are typically useful. Here we use `layout` to put the first plot above the second one, and `size` to reshape the plot size:
70+
```@example simulation_plotting
71+
plot(plt_X, plt_Y; layout = (2,1), size = (700,500))
72+
```
73+
74+
## [Saving plots](@id simulation_plotting_options)
75+
Once a plot has been saved to a variable, the `savefig` function can be used to save it to a file. Here we save our Brusselator plot simulation (the first argument) to a file called "saved_plot.png" (the second argument):
76+
```@example simulation_plotting
77+
plt = plot(sol)
78+
savefig(plt, "saved_plot.png")
79+
rm("saved_plot.png") # hide
80+
```
81+
The plot file type is automatically determined from the extension (if none is given, a .png file is created).
82+
83+
## [Phase-space plots](@id simulation_plotting_options)
84+
By default, simulations are plotted as species concentrations over time. However, [phase space](https://en.wikipedia.org/wiki/Phase_space#:~:text=In%20dynamical%20systems%20theory%20and,point%20in%20the%20phase%20space.) plots are also possible. This is done by designating the axis arguments using the `idxs` option, but providing them as a tuple. E.g. here we plot our simulation in $X-Y$ space:
85+
```@example simulation_plotting
86+
plot(sol; idxs = (:X, :Y))
87+
```

0 commit comments

Comments
 (0)