PGFPlotsX remove legend box (border)

Hi,
I am trying to remove the legend box in a plot with the pgfplotsx backend and I cannot find the right attribute. I know I can easily remove the box in the generated tex file (see here) but I would like to be able to do it directly in julia to avoid any manual changes if I have to rebuild the figure. I have found that I can change the color of the border with the foreground_color_legend attribute but that also changes the color of the text, which cannot be overwritten with the legendfontcolor attribute:

plot(0:0.1:1,0:0.1:1,foreground_color_legend=:white,legendfontcolor=:black)

gives me a figure with white border but also with white font. Ideally the border is not drawn at all. Any ideas how I can remove the box?

Hi @AAL . Using Plots and the pgfplotsx() backend, the trick is done by inserting fg_legend = :transparent into the plot function’s properties, like this

using Plots 
using LaTeXStrings # edditing with Latex
pgfplotsx() # pgfplotsx() backend
#gr()
f(x) = -2 + 10sin(x)
g(x) = -2 + 3x
q(x) = -10+x^2
plot(
    [f, g, q], 
    -5:0.01:5, 
    label=[L"f(x)" L"g(x)" L"q(x)"], 
    title = L"My \; Figure",
    xlabel = L"Peanuts",
    ylabel = L"\alpha (m/s)",
    fg_legend = :transparent # the line edges of the legend box disappear
)

The plot will look like this:

If you do it directly using PGFPlotsX, the trick is to insert legend_style= "{draw=none}" into the Axis properties, like this (sorry for raw LaTeX code at the top of the code, but it does not interfere in any way with what you want; it is intended to change the default layout of the plots in PGFPlotsX):

using PGFPlotsX
using LaTeXStrings

push!(PGFPlotsX.CUSTOM_PREAMBLE, 
    raw"\pgfplotsset{tick label style = {font = {\fontsize{12 pt}{12 pt}\selectfont}},
                     label style = {font = {\fontsize{12 pt}{12 pt}\selectfont}},
                     legend style = {font = {\fontsize{12 pt}{12 pt}\selectfont}},  
                    }"
       )

@pgf a = Axis({
              no_marks,
              legend_pos = "north west",
              legend_style= "{draw=none}", # To avoid the boxed legend style
              height = "10cm",
              width = "20cm",
              grid = "major",
              xlabel = L"\textbf{Date}",
              ylabel = L" \color{blue} CPI \ (\% change) \ , \ \color{red} FFR \ (\%)",
              x_tick_label_style="{/pgf/number format/1000 sep=}", # To avoid commas in the thousands
              ytick_distance ="5", # set the distance betwen each tick
              title = L"\textbf{Consumer Price Index and Federal Funds Rate (US: 1960:M1---2020:M6)}",
              xmax = 2022,
              xmin = 1958,
              ymax = 20,
              ymin = -5,
              },
         Plot(Table([Years, CPI]),
            {
            color = "blue",
            style = "{thick}"
            }),
         LegendEntry(L"CPI"),
    
         Plot(Table([Years, FFR]),
            {
            color = "red",
            style = "{thick}"
            }),
         LegendEntry(L"FFR"),
)
#pgfsave("PGFPlotsX_TimeSeries.pdf", a) #save plot as a pdf

The plot would look like this (sorry, you can not replicate the figure as you don’t have the data):

1 Like

Just a TeX nitpick: \( My \; Figure \) is wrong. If you have to be in math mode, go for \( \textrm{My Figure} \). There is really no reason for that title to be a LaTeXString to begin with. If your issue is that the font difference looks bad in the other backends, you are looking for default(fontfamily="Computer Modern").

(This goes the same for Peanuts)

1 Like

Hi @gustaphe, thank you. I know that titles do not necessarily need to use LaTeX, but sometimes they do because we may have to insert some mathematical symbol into it. That’s the reason I presented that strange type of title; the same for “Peanuts”. I tried to use your LaTeX proposal in Plots, but it does not work.

But this is not the main point here. I thought I was helping someone that had just arrived at the Julia world. @AAL did have a very usual and ordinary doubt about plotting (a pretty common thing in computation), but nobody did bother to help … for ten months. Yes, @AAL had posted this issue ten months ago. I just saw his /her issue because I had raised another issue a few days ago (related to PlotlyJS) and saw this issue at the top of the list. There must be something odd here because someone has a basic doubt about Plots, and nobody helps.

Within Plots.jl, the fg_legend=nothing argument does remove the legend box for all back-ends but unicodeplots (according to this list).

1 Like

@rafael.guerra , thanks. I had a typo in my first answer. In Plots, fg_legend = :transparent does the trick; in PGPlotsX it is legend_style= "{draw=none}". The two sets of code I presented above are correct, but in the introduction to the first one I had mentioned legend_style= "{draw=none}" and it should be fg_legend = :transparent. Correction done.

Thank you all for the answers. This is really helpful. I have used julia for a while to make my plots and as a workaround I was modifying the .tex files generated.

In the meantime I also wanted to make the legend box transparent. I am using fg_legend=:transparent and bg_legend=:transparent, which is exactly what I wanted. Just wanted to add this in case anybody lands here looking for the same behavior.

I could not find the fg_legend attribute in the Plots.jl documentation, including the list referenced by @rafael.guerra. Is there a more exhaustive documentation that I am missing?

2 Likes

That’s a shorthand for foreground_color_legend. In general the proper name of the keywords are not abbreviated.

@AAL, look no further than here.

1 Like