How to set the output size in `savefig()` in `GR.jl`

Hi,

I have a simple question about GR.jl (Not through Plots.jl).
I am displaying an image of size 300x300.

I want to display it on screen and save the plot with savefig:

using GR

img = rand(300, 300)

imshow(img)

savefig("img.png")

I expect the output to have size of 300x300 yet I get much bigger size.

How can I control the output size?

I saw Incorrect savefig size when using size = ( , ) · Issue #2303 · JuliaPlots/Plots.jl · GitHub yet I am not sure if it is related.

1 Like

An equivalent result (still) using the GR backend via Plots would be the following:

using Plots; gr()

img = rand(300, 300)
p = Plots.plot(img) # create your own here

plot!(size=(300,300))

Plots.savefig(p, "p.png") # note: savefig from Plots

Until you figure it out (or somebody can help with a pure GR solution), you can use this approach - in the end, it is still using the GR backend (I think GR is actually the default backend for Plots - so I don’t think you need to call gr()) specifically.

Later edit: By the way - without calling plot!(size=(300,300)), the Plots.savefig would also output a different size from 300x300. I think the size would be similar to the one produced by GR. I think a configuration method similar to the one for Plots should exist for GR (maybe it is not documented properly - I also looked at the tests, and the only thing they test for is for the resulting file to have a disk size larger than a certain value).

1 Like