# Modify axis options when using Plots.jl with pgfplotsx backend

**URL:** https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292
**Category:** Visualization
**Tags:** plotting, pgfplotsx
**Created:** [October 14, 2024, 2:46pm UTC](https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292 "2024-10-14T14:46:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lcm](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@lcm](https://discourse.julialang.org/u/lcm)
#### Post date: [October 14, 2024, 2:46pm UTC](https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292/1 "2024-10-14T14:46:13Z")

</div>

I am using Plots.jl with the `pgfplotsx()` backend for emitting `.tikz` files. Together with the `tikzscale` LaTeX package this makes for nice plots for a TeX document.  
However, in the emitted plots there are a lot of options on each `axis`. Amongst them are `height` and `width` as well as `font={{..}}` for the title, legend, labels and ticks. These all interfere with `tikzscale`, as the `width` will be set only when including the file and the font size should match the surrounding document style.

Is there a way to modify or delete these options before any `PGFPlotsX` code starts assembling the `.tikz` file? As I understand they are defaults being passed from `Plots.jl`. Suppose I have a single axis created by `p = plot(...)`, then `p.subplots[1].attr` holds all these options even before `PGFPlotsX` generated the plot. Deleting them from this `Dict` leads to errors though:

```julia
julia> delete!(p.subplots[1].attr, :titlefontsize);

julia> p
Error showing value of type Plots.Plot{Plots.PGFPlotsXBackend}:
ERROR: KeyError: key :titlefontsize not found

```

is there a way to remove some options? My current alternative would be some `sed` command or something to remove it from the `.tikz` file after generation.

I know this could be [easily controlled](https://kristofferc.github.io/PGFPlotsX.jl/stable/man/options/#Modifying-options-after-an-object-is-created) when using PGFPlotsX.jl directly, however I am changing the plots regularily and like being able to switch back and forth between `gr()` for plot modifications and `pgfplotsx()` for checking how it looks in the final document.

---

<div class="post-metadata">

### Author: ![lcm](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@lcm](https://discourse.julialang.org/u/lcm)
#### Post date: [November 4, 2024, 9:55am UTC](https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292/2 "2024-11-04T09:55:01Z")

</div>

Just in case anybody is interested, here is the function I use to remove above mentioned options from the generated `.tikz` file:

```julia
function clean_tikz(path)
    # match plot width and height attributes
    re_size = r"width={\d+\.\d+mm}, height={\d+\.\d+mm}, "

    # match font attributes
    re_font = r"font={{.+?}}, "

    # match colormap for heatmaps, commented out with default PGFPlotsX options
    re_colormap = r"%\\pgfplotsset{\n(% (colormap|rgb).+\n)+%}"

    tikz_plot = read(path, String)

    # remove size and font matches
    tikz_plot = replace(tikz_plot, re_size => "")
    tikz_plot = replace(tikz_plot, re_font => "")

    # uncomment colormap
    tikz_plot = replace(tikz_plot, re_colormap => x->replace(x, "%"=>""))

    # overwrite original file
    open(path, "w") do f
        write(f, tikz_plot)
    end

    nothing
end

```

---

<div class="post-metadata">

### Author: ![lcm](https://avatars.discourse-cdn.com/v4/letter/l/58956e/32.png) [@lcm](https://discourse.julialang.org/u/lcm)
#### Post date: [November 26, 2024, 9:46am UTC](https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292/3 "2024-11-26T09:46:46Z")

</div>

Small update to the colormap regex to also match independent of whitespaces before each line:

```julia
re_colormap = r"%[]*\\pgfplotsset{\n(%[]*(colormap|rgb).+\n)+%[]*}"

```

---

<div class="post-metadata">

### Author: ![thobbs](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@thobbs](https://discourse.julialang.org/u/thobbs)
#### Post date: [April 26, 2026, 4:31pm UTC](https://discourse.julialang.org/t/modify-axis-options-when-using-plots-jl-with-pgfplotsx-backend/121292/4 "2026-04-26T16:31:33Z")

</div>

This appears to still be an issue in 2026; I opened a github issue [1] since it seems like unintended behavior according to the workflow guide [2] in the Plots.jl documentation.

Thanks for sharing your function, it’s going to save me a lot of time 🙂

I added the following regex to also remove the line width options, so that the default line width of the TeX document is used:

```julia
...
re_linewidth = r"line width={\d+\.\d+}, |line width={\d+}, "
...
tikz_plot = replace(tikz_plot, re_linewidth => "")
...

```

```txt
[1] https://github.com/JuliaPlots/Plots.jl/issues/5737
[2] https://docs.juliaplots.org/dev/backends/#LaTeX-workflow

```
