JuliaPlots / PlotlyJS.jl

Julia library for plotting with plotly.js
Other
418 stars 77 forks source link

Figure management with unique identifier #397

Open eestdnt opened 3 years ago

eestdnt commented 3 years ago

Hi everyone,

Is it possible to open and update Electron figure windows between script executions similarly to the "figure" command in MATLAB?

I am looking for a method similar to the MATLAB's and matplotlib's figure() command, so that an internal collection of figures is managed using unique identifiers.

https://se.mathworks.com/help/matlab/ref/figure.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html

I often have MATLAB figure windows opened in one screen and my script in one screen where execution of the script updates the already opened windows instead of generating new windows.

The closest I have so far is pre-initializing some SyncPlot objects and use them as global variables in my script.

# Pre-initialization script, run only once to open the windows
using PlotlyJS, Blink;
figs = [plot(), plot()];
display(figs[1]);
display(figs[2]);
title(figs[1].window, "Figure 1");
title(figs[2].window, "Figure 2");

Modifying and executing _plotscript.jl many times only updates the windows.

# plot_script.jl
using PlotlyJS;

tv = 0:0.0005:0.1;
f = 50;

# Display sine wave in figure 1
addtraces!(figs[1], scatter(x=tv, y=sin.(2*pi*f*tv)));
update!(figs[1]);

# Display cosine wave in figure 2
addtraces!(figs[2], scatter(x=tv, y=cos.(2*pi*f*tv)));
update!(figs[2]);

It will be great to know if such built-in method exists or perhaps some nice feature to add to the awesome library.

sglyon commented 3 years ago

That's a neat idea.

Right now we don't have anything like that implemented. We require users to do as you say and manage the global state for keeping track of plots/windows on their own.

Each plot object will have a UUID as a field, so we already have a unique identifier we could use for referencing them.

I don't know that I'm totally understanding your use case. Looking at the second code block you presented, where does the figs object come from?

mzaffalon commented 3 years ago

I don't know that I'm totally understanding your use case. Looking at the second code block you presented, where does the figs object come from?

I guess from running the first snippet where he creates the vector containing the Plot objects.