Backends
Backends are the lifeblood of Plots, and the diversity between features, approaches, and strengths/weaknesses was one of the primary reasons that I started this package.
For those who haven't had the pleasure of hacking on 15 different plotting APIs: first, consider yourself lucky. However, you will probably have a hard time choosing the right backend for your task at hand. This document is meant to be a guide and introduction to make that choice.
At a glance
My favorites: GR
for speed, Plotly(JS)
for interactivity, UnicodePlots
for REPL/SSH and PyPlot
otherwise.
If you require... | then use... |
---|---|
features | PyPlot, Plotly(JS), GR |
speed | GR, UnicodePlots, InspectDR, Gaston |
interactivity | Plotly(JS), PyPlot, InspectDR |
beauty | Plotly(JS), PGFPlots/ PGFPlotsX |
REPL plotting | UnicodePlots |
3D plots | PyPlot, GR, Plotly(JS), Gaston |
a GUI window | GR, PyPlot, PlotlyJS, InspectDR |
a small footprint | UnicodePlots, Plotly |
backend stability | Gaston |
plot+data -> .hdf5 file | HDF5 |
Of course this list is rather subjective and nothing in life is that simple. Likely there are subtle tradeoffs between backends, long hidden bugs, and more excitement. Don't be shy to try out something new !
GR
The default backend. Very fast with lots of plot types. Still actively developed and improving daily.
Pros:
- Speed
- 2D and 3D
- Standalone or inline
Cons:
- Limited interactivity
Primary author: Josef Heinen (@jheinen)
Fine tuning
It is possible to use more features of GR
via the extra_kwargs
mechanism.
using Plots; gr()
x = range(-3, 3, length=30)
surface(
x, x, (x, y)->exp(-x^2 - y^2), c=:viridis, legend=:none,
nx=50, ny=50, display_option=Plots.GR.OPTION_SHADED_MESH, # <-- series[:extra_kwargs]
)
Supported :subplot
:extra_kwargs
Keyword | Description |
---|---|
legend_hfactor | Vertical spacing factor for legends |
Supported :series
:extra_kwargs
Series Type | Keyword | Description |
---|---|---|
:surface | nx | Number of interpolation points in the x direction |
:surface | ny | Number of interpolation points in the y direction |
:surface , :wireframe | display_option | see GR doc |
Plotly / PlotlyJS
These are treated as separate backends, though they share much of the code and use the Plotly JavaScript API. plotly()
is the only dependency-free plotting option, as the required JavaScript is bundled with Plots. It can create inline plots in IJulia, or open standalone browser windows when run from the Julia REPL.
plotlyjs()
is the preferred option, and taps into the great functionality of Spencer Lyon's PlotlyJS.jl. Inline IJulia plots can be updated from any cell... something that makes this backend stand out. From the Julia REPL, it taps into Blink.jl and Electron to plot within a standalone GUI window... also very cool. Also, PlotlyJS supports saving the output to more formats than Plotly, such as EPS and PDF, and thus is the recommended version of Plotly for developing publication-quality figures.
Pros:
- Tons of functionality
- 2D and 3D
- Mature library
- Interactivity (even when inline)
- Standalone or inline
Cons:
- No custom shapes
- JSON may limit performance
Primary PlotlyJS.jl author: Spencer Lyon (@spencerlyon2)
MathJax
Plotly needs to load MathJax to render LaTeX strings, therefore passing extra keywords with extra_kwargs = :plot
is implemented. With that it is possible to pass a header to the extra include_mathjax
keyword. It has the following options:
include_mathjax = ""
(default): no mathjax headerinclude_mathjax = "cdn"
include the standard online version of the headerinclude_mathjax = "<filename?config=xyz>"
include a user-defined file
These can also be passed using the extra_plot_kwargs
keyword.
using LaTeXStrings
plotlyjs()
plot(1:4, [[1,4,9,16]*10000, [0.5, 2, 4.5, 8]],
labels = [L"\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}";
L"\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}"] |> permutedims,
xlabel = L"\sqrt{(n_\text{c}(t|{T_\text{early}}))}",
ylabel = L"d, r \text{ (solar radius)}",
yformatter = :plain,
extra_plot_kwargs = KW(
:include_mathjax => "cdn",
:yaxis => KW(:automargin => true),
:xaxis => KW(:domain => "auto")
),
)
Fine tuning
It is possible to add additional arguments to the plotly series and layout dictionaries via the extra_kwargs
mechanism. Arbitrary arguments are supported but one needs to be careful since no checks are performed and thus it is possible to unintentionally overwrite existing entries.
For example adding customdata can be done the following way scatter(1:3, customdata=["a", "b", "c"])
. One can also pass multiple extra arguments to plotly.
pl = scatter(1:3, rand(3),
extra_kwargs = KW(
:series => KW(:customdata => ["a", "b", "c"]),
:plot => KW(:legend => KW(:itemsizing => "constant"))
)
)
PyPlot
A Julia wrapper around the popular python package PyPlot
(Matplotlib). It uses PyCall.jl
to pass data with minimal overhead.
Pros:
- Tons of functionality
- 2D and 3D
- Mature library
- Standalone or inline
- Well supported in Plots
Cons:
- Uses Python
- Dependencies frequently cause setup issues
Primary author: Steven G Johnson (@stevengj)
Fine tuning
It is possible to use more features of PyPlot/matplotlib
via the extra_kwargs
mechanism. For example, for a 3D plot, the following example should generate a colorbar at a proper location; without the extra_kwargs
below, the colorbar is displayed too far right to see its ticks and numbers. The four coordinates in the example below, i.e., [0.9, 0.05, 0.05, 0.9]
specify the colorbar location [ left, bottom, width, height ]
. Note that for 2D plots, this fine tuning is not necessary.
using Plots; pyplot()
x = y = collect(range(-π, π, length = 100))
fn(x, y) = begin
3 * exp(-(3x^2 + y^2)/5) * (sin(x+2y))+0.1*randn(1)[1]
end
surface(x, y, fn, c=:viridis, extra_kwargs=Dict(:subplot=>Dict("3d_colorbar_axis" => [0.9, 0.05, 0.05, 0.9])))
Supported :subplot
:extra_kwargs
Keyword | Description |
---|---|
3dcolorbaraxis | Specifying the colorbar location [ left, bottom, width, height ] for a 3D plot |
PGFPlotsX
LaTeX plotting, based on PGF/TikZ
.
Successor backend of PGFPlots backend.
Has more features and is still in development otherwise the same.
To add save a standalone .tex file including a preamble use attribute tex_output_standalone = true
in your plot
command.
Pros:
- Nice looking plots
- Lots of functionality (though the code is still WIP)
Cons:
- Tricky to install
- Heavy-weight dependencies
Authors:
- PGFPlots: Christian Feuersanger
- PGFPlotsX.jl: Kristoffer Carlsson (@KristofferC89), Tamas K. Papp (@tpapp)
- Plots <–> PGFPlotsX link code: Simon Christ (@BeastyBlacksmith), based on the code of Patrick Kofod Mogensen (@pkofod)
LaTeX workflow
To use the native LaTeX output of the pgfplotsx
backend you can save your plot as a .tex
or .tikz
file.
using Plots; pgfplotsx()
pl = plot(1:5)
pl2 = plot((1:5).^2, tex_output_standalone = true)
savefig(pl, "myline.tikz") # produces a tikzpicture environment that can be included in other documents
savefig(pl2, "myparabola.tex") # produces a standalone document that compiles by itself including preamble
Saving as .tikz
file has the advantage, that you can use \includegraphics
to rescale your plot without changing the size of the fonts. The default LaTeX output is intended to be included as a figure in another document and will not compile by itself. If you include these figures in another LaTeX document you need to have the correct preamble. The preamble of a plot can be shown using Plots.pgfx_preamble(pl)
or copied from the standalone output.
Fine tuning
It is possible to use more features of PGFPlotsX
via the extra_kwargs
mechanism. By default it interprets every extra keyword as an option to the plot
command. Setting extra_kwargs = :subplot
will treat them as an option to the axis
command and extra_kwargs = :plot
will be treated as an option to the tikzpicture
environment.
For example changing the colormap to one that is native to pgfplots can be achieved with the following. Like this it is possible to keep the preamble of latex documents clean.
using Plots; pgfplotsx()
surface(range(-3,3, length=30), range(-3,3, length=30),
(x, y)->exp(-x^2-y^2),
label="",
colormap_name = "viridis",
extra_kwargs =:subplot)