Setting aspect ratio in Plots with PGFPlotsX

wujinq
Thu, 31 Aug 2023 20:22:17 +0000
General Usage

What I want is to set the aspect ratio to 1 when plotting with Plots.jl and PGFPlotsX backend. My plotting script looks like this:

ε_min = -1.5
ε_max = 1

kpt_labels = [L"-\mathrm{Y}", L"\Gamma", L"\mathrm{Y}"]
kpt_pos = [-0.5, 0, 0.5]

p = plot(
    tex_output_standalone = true,
    grid = false,
    xlims = (-0.5, 0.5),
    ylims = (ε_min, ε_max),
    aspect_ratio = :equal,
    framestyle = :box,
    xtickfontsize=14,
    ytickfontsize=14,
    xguidefontsize=14,
    yguidefontsize=14,
    legendfontsize=14,
    xticks = (kpt_pos, kpt_labels),
    ylabel = L"\xi_{n \mathbf{k}}",
    foreground_color_legend = nothing,
    legend=:topleft,
)

followed by several lines of plot! statements.

By default, in the output TikZ code generated the follows can be seen:

\begin{axis}[
%...
width={150.4mm}, height={99.6mm},
%...

If the two options are set to the same value, then aspect ratio is already 1.

What is needed then is to realize this in the Julia script, without manually modifying the output LaTeX files.

I tried the follows but all of them fail:

1. aspect_ratio = :equal. This doesn’t change the output file at all. It seems the option is simply ignored.

2. axis_equal_image as is mentioned in aspect_ratio not working expectedly with PGFPlotsX · Issue #4766 · JuliaPlots/Plots.jl (github.com). The axis_equal_image option works for heatmap, but not plot.

3. When axis_equal_image is replaced by axis_equal, the plots represent the absolute values of x coordinates and y coordinates faithfully: thus, if the range of the x coordinate is -0.5-0.5 while the range of the y coordinate is 3, then we get a 1:3 plot; but the axes are still scaled according to the 150.4/99.6 ratio.

4. Setting height and width explicitly. When the following code is added to the plot statements, the resulting plot is completely nonsense:

plot!(p, x, y,
# ...
 height = 600,
 width = 600,
 extra_kwargs = :subplot,
)

I’m curious what’s the correct way to do it.

2 posts - 2 participants