PlotlyJS: how to change the default labels of variables

I have a kind of stupid question: how can we change the automatic labels of variables under PlotlyJS.jl? In the figure below, the names given by default are trace 0, trace 1 ,... , when instead I want GDP, INV, CONS and EXP. I know how to do it by creating individual scatter functions and then plotting everything together: p2 = plot([s1, s2, s3, s4], layout). However, this is not very efficient, and as a regular user of Plots.jl I would just need to type: labels = ["GDP" "INV" "CONS" "EXP"], to have the job done.

Is there any similar way in PlotlyJS.jl to do the trick? I tried restyle!(p2, names =["GDP" ,"CONS", "INV", "EXP"]) but it did not work. I tried other alternatives, but they also failed. Help will be very much appreciated.

The MWE is as follows:

p2 = plot(Period1, [GDP INV CONS EXP], 
        Layout(title = "Some macroeconomic aggregates: US (1960-Q1--2019-Q2)",
        xaxis_title = "Quarterly observations",
        xaxis_range = [1960, 2020],
        yaxis_title = "Billions US dollars",
        #yaxis_range=[-2, 2], 
        ))

which leads to:

The following restyling syntax does work:

using PlotlyJS

Period1 = 1950:2050; u = (Period1 .- 1950)/100;
GDP = u/2; INV = u; CONS = u*2; EXP = 2*u.^0.5;

p2 = PlotlyJS.plot(Period1, [GDP INV CONS EXP],
        Layout(title = "Some macroeconomic aggregates: US (1960-Q1--2019-Q2)",
        xaxis_title = "Quarterly observations",
        xaxis_range = [1960, 2020],
        yaxis_title = "Billions US dollars",
        yaxis_range=[0, 2])
)

restyle!(p2,1:4, name=["GDP", "INV", "CONS", "EXP"])

@rafael.guerra Thanks for your help.

I tried your solution (which I can see clearly works in your case), but I may have something missing on my computer that you don’t have. In my case, if I add the API function restyle! with the attributes that you stated, the code runs without any error … but the plot just vanishes in Jupyter, while in VSCode it does not change the plot in any way at all. The two figs below display these two mysterious cases.

Besides this problem, I have had no troubles at all with PlotlyJS.jl. I am running on a Windows 10 machine, Julia 1.6.1, PlotlyJS v0.14.1, and PlotlyBase v0.4.3. There must be a minor bug leading to this outcome. I used your entire example, and the outcome was the same.

In Jupyter:

In VSCode:

a7

Using here Julia 1.6.1, PlotlyJS v0.14.1, PlotlyBase v0.4.3 and VS Code on Win10.

Are you plotting to an external window? Probably you should, in order for the window to listen to parameter changes.
If you aren’t, please uncheck VS Code setting:

NB:
Tested running the code from the Julia 1.6.1 REPL and it worked fine too.

1 Like

Thanks a lot. In VSCode it RUNS perfectly well in the external window. I will see what happens in the Jupyter notebook. This is for teaching, so I need it to work in Jupyter (Pluto is not yet ready for PlotlyJS). Well, the Julia community is quite fascinating!

If you will, why not using the plotlyjs() back-end, within the comfort of Plots.jl syntax?

2 Likes

You can use the plotly() backend of Plots.jl inside Pluto.

2 Likes

@rafael.guerra @dpsanders, that’s what I will have to do, either using the plotlyjs() or the plotly() backends. I can do it in PlotlyJS as well, but its syntax is not as user-friendly as that of Plots.

I was not well aware of the functionality of displaying plots in an external window, and the gains we may get by using it. I migrated from Matlab where that was a default construction, and never questioned how to get it in Atom or VSCode. Working with PlotlyJS in an external window is like magic.

1 Like