Julia: How to save a figure without plotting/displaying it in PyPlot?
Asked Answered
P

2

7

I am using the PyPlot package in Julia to generate and save several figures. My current approach is to display the figure and then save it using savefig.

using PyPlot
a = rand(50,40)
imshow(a)
savefig("a.png")

Is there a way to save the figure without having to first display it?

Protozoal answered 18/9, 2016 at 20:44 Comment(2)
If you can sort out how to control the mpl backend, set it to 'Agg'.Consult
At a minimum, you can set the backend in your .matplotlibrc file.Consult
A
6

Are you using the REPL or IJulia?

If you close the figure then it won't show you the plot. Is that what you want?

a = rand(50,40)
ioff() #turns off interactive plotting
fig = figure()
imshow(a)
close(fig)

If that doesn't work you might need to turn off interactive plotting using ioff() or change the matplotlib backend (pygui(:Agg)) (see here: Calling pylab.savefig without display in ipython)

Remember that most questions about plotting using PyPlot can be worked out by reading answers from the python community. And also using the docs at https://github.com/JuliaPy/PyPlot.jl to translate between the two :)

Arce answered 18/9, 2016 at 22:42 Comment(0)
L
1

close() doesn't require any arguments so you can just call close() after saving the figure and create a new figure

using PyPlot
a = rand(50,40)
imshow(a)
savefig("a.png")
# call close
close()
Longlived answered 3/12, 2022 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.