2

I'd like to achieve that, using pgfplots, an axis is half as high as it is wide (or, in a slightly different scenario, half as high as it "should" be).

Consider this MWE

\documentclass[11pt,a4paper]{article} \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} \begin{document} \begin{figure} \centering \pgfplotsset{ width = 7cm } \begin{tikzpicture} \matrix{ \begin{axis} [ height = \pgfkeysvalueof{/pgfplots/width}*0.5, xticklabel = \empty ] \addplot coordinates {(1,1) (2,2)}; \end{axis} \\ \begin{axis} \addplot coordinates {(1,2) (2,1)}; \end{axis} \\ }; \end{tikzpicture} \end{figure} \end{document} 

This works, as long as width is explicitly set outside. How can I make this work, agnostic of whether width is set or not, falling back to \axisdefaultwidth if necessary.

1
  • The key value for width is not the actual width, which probably hasn't even been set when the key value for height is processed. Commented Feb 15 at 14:32

2 Answers 2

3

By default \axisdefaultwidth is 240pt and \axisdefaultheight 207pt. When you set only axis width or height, pgfplots will keeps the aspect ratio between \axisdefaultwidth and \axisdefaultheight.

EDIT

There is the key y post scale, but as pointed by @hpekristiansen in a comment this only scales the figure vertically, so y post scale=0.5 doesn't give exactly what you want.

Instead you can set the aspect ratio with

\def\axisdefaultheight{\dimeval{\axisdefaultwidth*0.5}}} 

Define a key to handle that

\pgfplotsset{ axis ratio/.code={\def\axisdefaultheight{\dimeval{\axisdefaultwidth*#1}}} } 

and use

\begin{axis}[ axis ratio={1/2} ] 

Example:

\documentclass[11pt,a4paper]{article} \usepackage{pgfplots} \pgfplotsset{compat=newest} \pgfplotsset{ axis ratio/.code={\def\axisdefaultheight{\dimeval{\axisdefaultwidth*#1}}} } \begin{document} \begin{tikzpicture} \begin{axis} [ width=240pt, height = 120pt, ] \addplot coordinates {(1,1) (2,2)}; \end{axis} \end{tikzpicture} \begin{tikzpicture} \begin{axis}[ axis ratio={1/2} ] \addplot coordinates {(1,2) (2,1)}; \end{axis} \end{tikzpicture} \end{document} 

Example

2
  • 1
    That does not work. y post scale=0.5 sets the height to half of what it else would have been. -not dependent of the width at all. You can see that your two graphs does not have the same height. Commented Feb 16 at 11:05
  • 1
    @hpekristiansen I didn't see that, thanks. I found a fix. I edited the answer. Commented Feb 16 at 21:00
1
\documentclass[tikz, border=1cm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \matrix{ \begin{axis}[ width=\axisdefaultwidth, height=0.5*\axisdefaultwidth, scale only axis=true, xticklabel=\empty, ] \addplot coordinates {(1,1) (2,2)}; \end{axis}\\ \begin{axis}[scale only axis=true] \addplot coordinates {(1,2) (2,1)}; \end{axis}\\ }; \end{tikzpicture} \end{document} 

Two graphs with different scaling

1
  • scale only axis=true is not needed - I am only guessing that is what you really want. Commented Feb 15 at 15:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.