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.

Julian functions

Plot and SyncPlot both have implementations of common Julia methods:

  • size: returns the width and layout attributes in the plot's layout
  • copy: create a shallow copy of all traces in the plot and the layout, but create a new divid

API functions

All exported functions from the plotly.js API have been exposed to Julia and operate on both Plot and 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
  • update!: combination of restyle! and relayout!
  • react!: In place updating of all traces and layout in plot. More efficient than constructing an entirely new plot from scratch, but has the same effect.
  • 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
  • purge!: completely remove all data and layout from the chart
  • 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 Plot 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.

Note

Be especially careful when trying to use restyle!, extendtraces!, and prependtraces! to set attributes that are arrays. The semantics are a bit subtle. Check the docstring for details and examples

Subplots

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 vcat, hcat, and 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 p1 and p2 as subplots side-by-side, we would do

[p1 p2]

which would be displayed as

If instead we wanted two rows and one column we could

[p3, p4]

Finally, we can make a 2x2 grid of subplots:

[p1 p2
 p3 p4]

Saving figures

In order to save figures in other formats you must you need to have the ORCA.jl package installed and loaded. To install this, call add ORCA from the Julia package manager REPL mode (which you can enter by pressing ] at the REPL). To load, call using ORCA.

After loading you can also save figures in a variety of formats:

savefig(p::Union{Plot,SyncPlot}, "output_filename.pdf")
savefig(p::Union{Plot,SyncPlot}, "output_filename.html")
savefig(p::Union{Plot,SyncPlot}, "output_filename.json")
savefig(p::Union{Plot,SyncPlot}, "output_filename.png")
savefig(p::Union{Plot,SyncPlot}, "output_filename.eps")
savefig(p::Union{Plot,SyncPlot}, "output_filename.svg")
savefig(p::Union{Plot,SyncPlot}, "output_filename.jpeg")
savefig(p::Union{Plot,SyncPlot}, "output_filename.webp")

Note

You can also save the json for a figure by calling savejson(p::Union{Plot,SyncPlot}, filename::String). This does not require the ORCA.jl package.