Can't get rid of legend in Plotlyjs.jl

Hi all

I am using

julia 1.8.2
PlotlyJS v0.18.10

I want to remove the legend from this plot

I tried using

using PlotlyJS

make_plot() = plot(scatter(x=1:10, y=rand(10)),
                   Layout(yaxis_visible=false,xaxis_visible=false,  showlegend=false, )
                   )

fig = [
make_plot() make_plot() 
 ]

showlegend = false does nothing what am I doing wrong please?

Here are my proposals. You can add showlegend = false also as part of the scatter function.

using PlotlyJS

function plot_two_graphs()
    _plt = PlotlyJS.make_subplots(; rows = 1, cols = 2,
        specs = [ PlotlyJS.Spec() PlotlyJS.Spec()]
    )
    _plt_trace = PlotlyJS.scatter(; x = 1:10, y=rand(10), showlegend = false)
    PlotlyJS.add_trace!(_plt, _plt_trace, row=1, col=1)  
    PlotlyJS.add_trace!(_plt, _plt_trace, row=1, col=2) 
    return _plt
end

# --- PlotlyJS objects
plt_trace       = PlotlyJS.scatter(x=1:10, y=rand(10), showlegend = false)
plt_trace_wl    = PlotlyJS.scatter(x=1:10, y=rand(10), showlegend = true)
plt_layout      = PlotlyJS.Layout(yaxis_visible= false, xaxis_visible= false,  showlegend= false, )
# ---
make_plot()     = PlotlyJS.Plot(plt_trace, plt_layout)
make_plot_wl()  = PlotlyJS.Plot(plt_trace_wl, plt_layout)
# ---
fig_A = [make_plot() make_plot() ]
fig_B = [make_plot_wl() make_plot()  ]
fig_C = plot_two_graphs()

julia> fig.plot.layout
layout with fields margin, template, xaxis1, xaxis2, yaxis1, and yaxis2

The [...] or hcat function for plots ignores showlegend (and possibly also other layout configurations).

julia> relayout!(fig, showlegend=false)

fixes this in your case. The [...] or hcat functions are Julia only methods for creating subplots. The Plotly-supported methods are more tedious, but are flexible and perhaps have less surprises.

2 Likes

WOW @ellocco

thank you so much for taking the time to give me so many approaches to check out. Is there a reason that just putting showlegend=false into the Layout call doesn’t work? It just seems cleaner to me.

plot(scatter(x=1:10, y=rand(10)), Layout(yaxis_visible=false,xaxis_visible=false,  showlegend=false, ))

thanks for the EXCELLENT examples.

thank you @stephancb
I’ll cerainly look into hcat but I am on the cusp of bypassing Plotlyjs and using Plots instead. I’m hitting all sorts of issues with Plotlyjs and as a noob I like simplicity. Thank you for your reply it has given me something to look into. I would not have determined that hcat would be involved in this.
thanks again

What is really cool, are the interactive html-plots that you can generate with Plotly/PlotlyJS.
What I also like is the good support for multiple axis.
If you would like to have fancy LaTeX-formula in your figures you might skip PlotlyJS,
as currently the LaTeX-support does only work for html-output.
I use PlotlyJS also for static vector graphic plots (*.pdf, *.svg).
Below an example with transparent background and greek letters,
it demonstrates also the limitations of PlotlyJS.
The question about showlegend=false inside the Layout-section was already
commented by @stephancb:

using PlotlyJS
# --- PlotlyJS Objects:
plt_trace_wl    = PlotlyJS.scatter(x=1:10, y=rand(10), showlegend = true)
plt_layout      = PlotlyJS.Layout(yaxis_visible= false, xaxis_visible= false,  showlegend= false, )
hdl_plt         = PlotlyJS.Plot(plt_trace_wl, plt_layout)
PlotlyJS.savefig(hdl_plt, "./plt/test.html")

# --- relayout plot with transparent background:
PlotlyJS.relayout!(hdl_plt,
    height          = 400,
    width           = trunc(Int, sqrt(2) * 400),
    title_x         = 0.5, # center title
    title_font_size = 28,
    title_text      = "α  <b>transparent</b><sub>Θ</sub><sup>background</sup>",
    template        = templates.plotly_white, 
    paper_bgcolor   = "rgba(0,0,0,0)", # transparent background
    plot_bgcolor    = "rgba(0,0,0,0)", # transparent background
)
PlotlyJS.savefig(hdl_plt, "./plt/test.svg")
1 Like

Hi there @ellocco
thank you so much for taking the time to give a noob things to consider based on experience. I’m not expecting to use Latex in any way as my needs are VERY simple as I adhere to Tufte rules.

thank you so much for the EXCELLENT example code, this REALLY helps a noob as it gives us something to tinker with. I’ll see if I can just get it to plot from the cli and not save it. There seems to be a difference between running Plotlyjs code in the cli and the REPL, yet ANOTHER learning opportunity :slight_smile:

I’m going to look into the static vector plot side of things thank you for pointing that out.

To be honest I am not sure that I understand why “showlegend=false” doesn’t work in my code example. I read @stephancb post but can’t see where that question is addressed. I’m going to follow up with his approach this weekend when I start to experiment with PlutoHooks.jl

to a noob like me

Layout(yaxis_visible=false,xaxis_visible=false,  showlegend=false, )

seems rational and cleaner. The word Layout translates into the real world as do the parameter names and values.

thanks again for giving me food for thought.

The way I see it: PlotlyJS.jl is not yet mature, especially the documentation,
often you need to relay on the examples written in Python and transfer this into the Julia notation.
If I understand @stephancb correctly, his advice is:
if things do not work as expected, try PlotlyJS.relayout!() instead.

1 Like

thank you so much for all your help @ellocco and to you @stephancb

I’m going to look into your wonderful examples and @stephancb observations this weekend. Right now I’m wondering if Plots.jl is a better choice given that I tend to strive for simplicity. All I wanted to do was to produce a 10 by 100 subplot with a transparent background. I seem to remember that I did it in python using matplotlib in 10 minutes. That goes to your maturity observation.

I’m VERY enthuastic about Pluto and PlutoHooks and so that’s another component to this app I am considering for julia.

thanks again for all your help

Sorry, if I have been too terse:

To control the legend you need to set it after combining the subplots to one:

julia> relayout!(fig, showlegend=false)

This is because the [...], which is equivalent to hcat and vcat functions, as overloaded by PlotlyJS.jl does not carry over layout parameters of the subplots.

One of the advantages of PlotlyJS for me is that I can save plots in JSON format for later editing and use. For example, if I need to make presentation slides showing plots that I had previously published in documents and papers, typically the font size of labels and annotations need to be significantly enlarged for better viewing by an audience. Such edits are quite easy with PlotlyJS, as deleting the legend above shows.

1 Like

You kidding ? It’s amazing to me that you take time to help noobs like me, thanks so much for that. It really does make a difference as learning julia coming from python is a little hard.

I was going to investigate the […] this weekend and track down what you were highlighting. I’ve been USING julia but not digging into HOW it works. I see this as a learning opportunity. I am still puzzled as to why

Layout(yaxis_visible=false,xaxis_visible=false,  showlegend=false, )

doesn’t work but I can move on from that once I understand how you approach works.

My intent is to use a zmq stream of ipms to populate a dataframe and place it in a plutohooks cell then using the Pluto’s web api to perform external operations and control the plotting via the notebook.

As you can see, you usecase is mainly static and mine is dynamic.

I want to make sure that your, and the rest of the discourse community, efforts to help people like me ( new to julia coming from python) is so immensely helpful. Without people like you I certainly would have given up when I first started. I can only spend about 1 - 2 hours on actual coding julia a month so though I have been using it for a year I only have 10 - 15 hours of self directed learning. You can see that ANY experienced help will be so appreciated.

1 Like

There is also a related thread in the package issues blog:
# Subplot layout editing issues #277

1 Like

Hi @ellocco

thank you SO much for all your support in this issue. I’m going to mark this issue as solved as you both @ellocco and @stephancb advised me to pursue relayout! as the best solution to my issue.

Your worked examples were a positive boon to my and I thank you for taking the time. I use mwe as a learning experience and yours REALLY helped.

have an excellent weekend.

dent