GR.jl Open two figures

Hi,

I am using GR.jl and would like to open two figures. I also need to toggle between them in order to show changes of real-time data. In PyPlot.jl I would use figure(1) and figure(2) to switch between the figures.

Thanks,

Tobias

Ping: @jheinen

2 Likes

If you are willing to use Plots.jl, the following should work:

plt = plot(title="My two figures", layout=(1,2))

# update figure 1
plot!(plt[1], rand(10), rand(10), kwargs...)

# update figure 2
histogram!(plt[2], rand(100))

# more figure 1
vline!(plt[1], [1,2,3])

I am not sure however how you can clean one of the subplots in the middle of the process. The commands with a bang (!) only append to the scene.

thanks, but Plots.jl is not an option. My package load time is already large and Plots.jl adds too much overhead.

The loading time of your package should be independent of Plots.jl if you implement the functionality with a recipe and RecipesBase.jl. I don’t know how to implement it easily though, recipes aren’t that trivial to digest.

So in the end, is displaying multiple figures possible if I directly use GR.jl then?

I did try Plots.jl but I felt the behavior over different backends are a bit different (sometimes quite different) and I always found myself wasting time switching back and forth fighting differences in features and behaviors. So my current approach is to use GR.jl (whose grammar is closest to matplotlib and Matlab) and try to do as much as it supports, then forget about the things it does not support.

This is old, but I cannot find any enhancement. Even using Plots, this solution does not really create two figures as asked by the OP (I tested only the gr backend), but rather a single figure with two subplots. Is there actually a way to have two separate figures (independent canvas) with gr()?

Thanks

Not directly with GR yet, but using GRUtils you can deal with multiple figures:
https://heliosdrm.github.io/GRUtils.jl/stable/multiple/

Limitations:

  • GRUtils is a prototype of an alternative “high level” interface for GR, not a plotting backend, so unlike GR, it cannot be used with Plots.jl.
  • As explained in the linked documentation, with GRUtils you can have different Figures in your workspace, manipulate them at the same time, and switch back and forth between them as the “current figure”. But it uses the same plotting devices as GR. As far as I know, the GKS plotting device only allows you to open one plotting window; you can’t have two or more plotting windows in front of you at the same time, if that’s what you are looking for.

(Disclaimer: I’m the author of GRUtils.)

2 Likes

Thanks for the answer (and for coding the Utils). It was surprisingly difficult to find info on this.

Not sure what happened, but using GRUtils messed up my dependencies. Switched back to Plots with pyplot backend seems to be the best solution for now

The only (direct) restriction set by GRUtils is the version of GR, which must be anyone between 0.42 and 0.49 (current). Can you check ]st GR to see what version of GR you have in your environment?

If it’s too messy, maybe you should try a clean environment for your project. See:
https://julialang.github.io/Pkg.jl/stable/environments/

In short: cd to a folder that does not belong to any other Julia project, and from the REPL:

] activate .
# Now `] st` should tell you are in an empty environment
] add GRUtils
# And `]add` other dependencies you might need

When you do your tests, be sure that when you are using any package you have previously ]activate-d the enviornment you want to work on, and the package has not been previously loaded in that session.