0

I am trying to draw a plot as following:

What I have

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
    \begin{axis} [
            axis lines=center,
            width=25cm,
            height=10cm,
            xmin=0,
            ymin=0,
            xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
            ytick={0,5,10},
            clip=false,
            xmajorgrids,
            ymajorgrids,
            grid style=dashed,
            %%%%%%% FIRST ATTEMPT
            %axis equal image,
            %%%%%%% SECOND ATTEMPT
            %x=.5cm,
            %y=.5cm,
            %%%%%%% THIRD ATTEMPT
            %width=24cm,
            %height=10cm,
        ]
        \addplot coordinates {(0,0)(5,5)(8,10)(24,0)};
    \end{axis}
\end{tikzpicture}

\end{document}

Since there are less "points?" on y-axis than x-axis, the graph has a rectangular grid. I would like to draw the plot with square grids i.e. something like:

What I want

I have tried several ways from here and here with no success. The attempts are commented on the code.

How can we achieve the desired result? What is the formula for scaling?

0

1 Answer 1

4

Try unit vector ratio*={5 1 1}. Note this overwrites height=10cm.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
    \begin{axis} [
            axis lines=center,
            width=25cm,
            height=10cm,
            unit vector ratio*={5 1 1},  % <<< added
            xmin=0,
            ymin=0,
            xtick={0,1,...,24},
            ytick={0,5,10},
            clip=false,
            xmajorgrids,
            ymajorgrids,
            grid style=dashed,
            %%%%%%% FIRST ATTEMPT
            %axis equal image,
            %%%%%%% SECOND ATTEMPT
            %x=.5cm,
            %y=.5cm,
            %%%%%%% THIRD ATTEMPT
            %width=24cm,
            %height=10cm,
        ]
        \addplot coordinates {(0,0)(5,5)(8,10)(24,0)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

3
  • Thanks! It worked. But why the formula {5 1}? I mean I don't know why that works; From the PGFPLOTS manual, "Thus, a three dimensional axis with unit vector ratio=1 2 4 will have an x unit which is 1/4 the length of the z unit, and a y unit which is 2/4 the length of the z unit" (p. 302). In our case it is traduced as "The x unit will be 5/1 times the length of y unit", but why 5?
    – manooooh
    Commented Jun 13, 2021 at 5:24
  • 2
    Because every square grid is expected to occupy 1 x unit and 5 y units. Commented Jun 13, 2021 at 5:59
  • I had the same problem and used unit vector ratio*={10 1 1} because my x axis ticks had a spacing of 1 and my y axis ticks had a spacing of 10. +1 and thank you for the answer!
    – orion2112
    Commented Apr 10 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .