# PGFPlotsX remove legend box (border)

**URL:** https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992
**Category:** Visualization
**Tags:** plotting, pgfplotsx
**Created:** [August 15, 2020, 1:41pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992 "2020-08-15T13:41:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AAL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aal/32/9242_2.png) [@AAL](https://discourse.julialang.org/u/AAL)
#### Post date: [August 15, 2020, 1:41pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/1 "2020-08-15T13:41:03Z")

</div>

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](https://tex.stackexchange.com/questions/7011/removing-legend-box-border-in-pgfplots)) 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:

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

---

<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: [June 13, 2021, 10:42am UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/2 "2021-06-13T10:42:29Z")

</div>

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

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

 ![aa1](https://global.discourse-cdn.com/julialang/original/3X/1/8/188d83af17e81c4adbcda8d01e70cb9fcf3c51dd.png)

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

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

 ![aa2](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6af03aa578d3b14e2bda24cb527801cae81f7e6.png)

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 13, 2021, 1:15pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/3 "2021-06-13T13:15:04Z")

</div>

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`)

---

<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: [June 13, 2021, 2:36pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/4 "2021-06-13T14:36:14Z")

</div>

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.

---

<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: [June 13, 2021, 2:51pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/5 "2021-06-13T14:51:11Z")

</div>

Within Plots.jl, the `fg_legend=nothing` argument does remove the legend box for all back-ends but `unicodeplots` (according to [this list](https://docs.juliaplots.org/latest/generated/supported/)).

---

<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: [June 13, 2021, 3:08pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/6 "2021-06-13T15:08:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![AAL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aal/32/9242_2.png) [@AAL](https://discourse.julialang.org/u/AAL)
#### Post date: [June 15, 2021, 1:43pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/7 "2021-06-15T13:43:55Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 15, 2021, 1:59pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/8 "2021-06-15T13:59:49Z")

</div>

> [@AAL](#):
>
> I could not find the `fg_legend` attribute in the Plots.jl documentation,

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

---

<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: [June 15, 2021, 2:12pm UTC](https://discourse.julialang.org/t/pgfplotsx-remove-legend-box-border/44992/9 "2021-06-15T14:12:44Z")

</div>

@AAL, look no further than [here](https://docs.juliaplots.org/latest/generated/attributes_subplot/).
