Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] savefig() not working properly in local (function) scope - PGFPlotsX #4943

Closed
GustavoGarone opened this issue Jun 13, 2024 · 1 comment
Labels

Comments

@GustavoGarone
Copy link

GustavoGarone commented Jun 13, 2024

Thanks for checking my Issue! I'm having trouble saving plots through savefig() while in a local (function) scope.

Details

I was trying to plot different data and save them to different PDFs, but when plotting them individually like in

function main()
# ...
plEstimado = plot(estimado, label = ["V. Est."])
savefig("~/graph.pdf")
# ...
end

All of them save blank with a warning:

Warning: Tikz document is empty
└ @ PGFPlotsX ~/.julia/packages/PGFPlotsX/4g8BA/src/tikzdocument.jl:128

And when trying to plot them together:

function graphMultiple(plEstimado, plEsperado, plDiff, item)
# This function is called many times in main to generate different graphs with different datasets.
  graficos = plot(plEstimado, plEsperado, plDiff,
        layout=@layout([a b; c]), title = ["Estimado" "Esperado" "Diferença"],
        titlelocation = :left)
  savefig(graficos, "~/GraficoDados$item.pdf") 
end

I get a crash with the following dump:

ERROR: LoadError: type Nothing has no field was_shown
Stacktrace:
[1] setproperty!(x::Nothing, f::Symbol, v::Bool)
@ Base ./Base.jl:39
[2] _show(io::IOStream, mime::MIME{Symbol("image/png")}, plt::Plots.Plot{Plots.PGFPlotsXBackend})
@ Plots ~/.julia/packages/Plots/ju9dp/src/backends/pgfplotsx.jl:1370
[3] #invokelatest#2
@ ./essentials.jl:892 [inlined]
[4] invokelatest
@ ./essentials.jl:889 [inlined]
[5] show
@ ~/.julia/packages/Plots/ju9dp/src/output.jl:232 [inlined]
[6] #347
@ ~/.julia/packages/Plots/ju9dp/src/output.jl:6 [inlined]
[7] open(::Plots.var"#347#348"{Plots.Plot{Plots.PGFPlotsXBackend}}, ::String, ::Vararg{String}; kwargs::@kwargs{})
@ Base ./io.jl:396
[8] open
@ ./io.jl:393 [inlined]
[9] png(plt::Plots.Plot{Plots.PGFPlotsXBackend}, fn::String)
@ Plots ~/.julia/packages/Plots/ju9dp/src/output.jl:6
[10] savefig(plt::Plots.Plot{Plots.PGFPlotsXBackend}, fn::String)
@ Plots ~/.julia/packages/Plots/ju9dp/src/output.jl:149
[11] graficar(plEstimado::Plots.Plot{Plots.PGFPlotsXBackend}, plEsperado::Plots.Plot{Plots.PGFPlotsXBackend}, plDiff::Plots.Plot{Plots.PGFPlotsXBackend}, item::Int64)
@ Main ~/Projetos/Estimador-de-Passeio/testadorDemo.jl:62
[12] main()
@ Main ~/Projetos/Estimador-de-Passeio/testadorDemo.jl:48
[13] top-level scope
@ ~/Projetos/Estimador-de-Passeio/testadorDemo.jl:65
in expression starting at /home/minze/Projetos/Estimador-de-Passeio/testadorDemo.jl:65

My current Workaround

I have been using savefig() in global scope, as that seems to fix the issue, but I'd prefer if I was able to do it in local scope for performance and organizational reasons.

I'd rather not switch backends for compability issues with my OS and my preference for LaTeX, if at all possible.

Backends

I have only used the gr (inside an ubuntu container in distrobox) and pgfplotsx backends, and this issue hasn't happened with gr before, but that was different code.
This bug occurs on:

Backend yes no untested
gr (default) X
pythonplot X
plotlyjs X
pgfplotsx X
unicodeplots X
inspectdr X
gaston X

Versions

Plots.jl version: 1.40.4
PGFPlotsX version: 1.6.1
Output of versioninfo():
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: 8 × 12th Gen Intel(R) Core(TM) i3-1215U
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-15.0.7 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

My system

I'm using NixOS stable with the julia-bin package (although my Julia packages are managed through Pkgs) with home-manager and flakes.

@GustavoGarone GustavoGarone changed the title [BUG] [BUG] savefig() not working properly in local (function) scope - PGFPlotsX Jun 13, 2024
@GustavoGarone GustavoGarone changed the title [BUG] savefig() not working properly in local (function) scope - PGFPlotsX [BUG] savefig() not working properly in local (function) scope - PGFPlotsX Jun 13, 2024
@GustavoGarone
Copy link
Author

I have managed to fix it, I just had a misunderstanding of how choosing your backend works. It should be like this:

using Plots
pgfplots()
function main()
#...

and NOT

using Plots
function main()
pgfplotsx()
#...

As I was doing, unless you call pgfplotsx() in every scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant