Save a figure with a specific size with a subplot of a specific size in Plots.jl (with PGFPlotsX backend)

In PyPlot, I can output a figure with a specific size containing an axis in the figure with a specific size using the code:

using PyPlot
fig_width = 4.0
fig_height = 3.0
fig = figure( figsize = (fig_width, fig_height) )
ax = plt.axes( [0.5,0.5,0.1,0.1] )
ax.plot( [0,1] , [0,1] )
savefig("pyplot_output.pdf")

This will create a pdf that is 4 inches wide and 3 inches high, and an axis that has its lower left corner in the middle of the figure and is 10% of the figure wide and 10% of the figure high.

Is it possible to to achieve the same outcome using Plots.jl with the PGFPlotsX backend? I.e., can I 1: Specify the output size of the figure? 2: Specify the relative size of the axis within the figure?

I feel a bit silly asking such a basic question, but even after reading the Plots.jl documentation and Googling around, I could not figure out how to do this.

You can pass backend-specific keyword arguments from Plots.jl using extra_kwargs. For example:

using Plots; pgfplotsx()
fig_width = 4.0
fig_height = 3.0

plot(Plots.fakedata(50, 5), extra_kwargs =Dict(:subplot=>Dict("height" => "$(fig_height)in", "width" => "$(fig_width)in")))

See:
https://docs.juliaplots.org/stable/backends/#Fine-tuning-4
https://docs.juliaplots.org/stable/generated/attributes_plot/
https://kristofferc.github.io/PGFPlotsX.jl/stable/examples/gallery/#Mixing-expression-and-coordinates

1 Like

Thanks! I’ll have a look and see if I can figure it out. The particular example does unfortunately not quite work–it produces a pdf that is approximately 4.33 times 2.68 inches big.