Working with plots
There are various methods defined on the
Plot type. We will cover a few of
them here, but consult the (forthcoming) API docs for more exhaustive coverage.
SyncPlot both have implementations of common Julia methods:
size: returns the
layoutattributes in the plot's layout
copy: create a shallow copy of all traces in the plot and the layout, but create a new
All exported functions from the plotly.js
API have been
exposed to Julia and operate on both
SyncPlot instances. Each of
these functions has semantics that match the semantics of plotly.js
In PlotlyJS.jl these functions are spelled:
restyle!: edit attributes on one or more traces
relayout!: edit attributes on the layout
addtraces!: add traces to a plot at specified indices
deletetraces!: delete specific traces from a plot
movetraces!: reorder traces in a plot
redraw!: for a redraw of an entire plot
There are also the two unexported (but still callable) methods from plotly.js
extendtraces!: Extend specific attributes of one or more traces with more data by appending to the end of the attribute
prependtraces!: Prepend additional data to specific attributes on one or more traces
When any of these routines is called on a
SyncPlot the underlying
object (in the
plot field on the
SyncPlot) is updated and the plotly.js
function is called. This is where
SyncPlot gets its name: when modifying a
plot, it keeps the Julia object and the display in sync.
For more details on which methods are available for each of the above functions consult the docstrings or (forthcoming) API documentation.
Be especially careful when trying to use
restyle! to set attributes that
are arrays. The semantics are a bit subtle. Check the docstring for details
A common task is to construct subpots, or plots with more than one set of axes. This is possible using the declarative plotly.js syntax, but can be tedious at best.
PlotlyJS.jl provides a conveient syntax for constructing what we will call regular grids of subplots. By regular we mean a square grid of plots.
To do this we will make a pun of the
hvcat functions from
Base and leverage the array construction syntax to build up our subplots.
Suppose we are working with the following plots:
p1 = Plot(scatter(;y=randn(3))) p2 = Plot(histogram(;x=randn(50), nbinsx=4)) p3 = Plot(scatter(;y=cumsum(randn(12)), name="Random Walk")) p4 = Plot([scatter(;x=1:4, y=[0, 2, 3, 5], fill="tozeroy"), scatter(;x=1:4, y=[3, 5, 1, 7], fill="tonexty")])
If we wanted to combine
p2 as subplots side-by-side, we would do
which would be displayed as
If instead we wanted two rows and one column we could
Finally, we can make a 2x2 grid of subplots:
[p1 p2 p3 p4]
Right now saving figures to a file only works when using the
The following syntaxes are currently supported without any other packages
# save to html. `js` is a keyword argument that specifies how plotly.js can be # included. Supported values are `:local`, `:remote`, `:embed`. See docstring # for more details savefig(sp::ElectronPlot, "output_filename.html"; js::Symbol) # save svg savefig(sp::ElectronPlot, "output_filename.svg")
Other routines are available for saving figures, but they require the independent installation of various Julia packages.
If you have installed Rsvg.jl
you can use the following routines:
PlotlyJS.savefig(sp::ElectronPlot, "output_filename.pdf") PlotlyJS.savefig(sp::ElectronPlot, "output_filename.png") PlotlyJS.savefig(sp::ElectronPlot, "output_filename.eps")
Note that the pdf and eps (not png) export from this function this case will be a true vector image. This allows you to have infinitely zoomable quality. This is the recommended way to obtain a pdf of your plot, but comes with the extra step of installing Rsvg.jl via:
If you have ImageMagick.jl properly installed in your Julia installation you can also do the following:
savefig_imagemagick(sp::ElectronPlot, "output_filename.png") savefig_imagemagick(sp::ElectronPlot, "output_filename.pdf") savefig_imagemagick(sp::ElectronPlot, "output_filename.jpeg")
Please note that the maximum DPI resolution for any of these formats is 96.
To get true vector quality pdf files, we recommend using the Rsvg backend.
There is one more routine you can use to save a figure to a file. This routine
requires that you have properly installed the cairosvg
python package and that the
cairosvg command is on your PATH.
cairosvg can be found, you can use the following routines:
PlotlyJS.savefig_cairosvg(sp::ElectronPlot, "output_filename.png") PlotlyJS.savefig_cairosvg(sp::ElectronPlot, "output_filename.pdf") PlotlyJS.savefig_cairosvg(sp::ElectronPlot, "output_filename.ps")