# Setting aspect ratio in Plots with PGFPlotsX

**URL:** <https://discourse.julialang.org/t/setting-aspect-ratio-in-plots-with-pgfplotsx/103424>\
**Category:** General Usage\
**Tags:** plotting, pgfplotsx\
**Created:** [August 31, 2023, 8:22pm UTC](https://discourse.julialang.org/t/setting-aspect-ratio-in-plots-with-pgfplotsx/103424 "2023-08-31T20:22:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wujinq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wujinq/32/22221_2.png) [@wujinq](https://discourse.julialang.org/u/wujinq)\
**Post date:** [August 31, 2023, 8:22pm UTC](https://discourse.julialang.org/t/setting-aspect-ratio-in-plots-with-pgfplotsx/103424/1 "2023-08-31T20:22:17Z")

</div>

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:

```julia
ε_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:

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

```

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

 ![correct](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a4e85cb20fb51fb46a4b51dada24e9e47ea4c99.png)

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)](https://github.com/JuliaPlots/Plots.jl/issues/4766). 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:

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

```

 ![case 3](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5daf8f92b28d96b6b551ba7e897d9cf26b8b81f.png)

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

---

<div class="post-metadata">

**Author:** ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)\
**Post date:** [April 5, 2025, 10:25am UTC](https://discourse.julialang.org/t/setting-aspect-ratio-in-plots-with-pgfplotsx/103424/2 "2025-04-05T10:25:49Z")

</div>

Do not specify all three of `aspect_ratio = :equal`, `xlims`, and `ylims`, as these are conflicting requirements and the two latter ones win.
