# PlotlyJS: how to change the default labels of variables

**URL:** https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798
**Category:** General Usage
**Tags:** plotting, plotlyjs
**Created:** [May 8, 2021, 11:31pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798 "2021-05-08T23:31:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [May 8, 2021, 11:31pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/1 "2021-05-08T23:31:48Z")

</div>

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:

```julia
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:

 ![a4](https://global.discourse-cdn.com/julialang/original/3X/b/1/b1ccecf7b53373935cced7eb45980470a1ee1145.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 9, 2021, 8:01pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/2 "2021-05-09T20:01:05Z")

</div>

The following restyling syntax does work:

```julia
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"])

```

 ![PlotlyJS_edit_labels_restyle](https://global.discourse-cdn.com/julialang/original/3X/d/c/dcd48fd8fc4ba14e3d234deca970495335aae821.png)

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [May 9, 2021, 9:26pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/3 "2021-05-09T21:26:38Z")

</div>

@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:

 ![a6](https://global.discourse-cdn.com/julialang/original/3X/d/8/d8753254cce48fc74e522da30a5c94d1e30cb721.png)

In VSCode:

![a7](https://global.discourse-cdn.com/julialang/original/3X/6/4/6409aa6437d152716b2a97512a187056edd38fc1.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 9, 2021, 9:50pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/4 "2021-05-09T21:50:44Z")

</div>

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:

 ![VS_Code_Julia_Plot_Pane](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c04a321ba06a038d454b283d8222d6b1022a974.png)

**NB:**  
_Tested running the code from the Julia 1.6.1 REPL and it worked fine too._

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [May 9, 2021, 10:02pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/5 "2021-05-09T22:02:22Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 10, 2021, 12:36pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/6 "2021-05-10T12:36:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 10, 2021, 1:01pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/7 "2021-05-10T13:01:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [May 10, 2021, 2:57pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-change-the-default-labels-of-variables/60798/8 "2021-05-10T14:57:25Z")

</div>

@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.
