3

So I have been trying to graph a function for my economics class. My problem is that the "axis" that are created do not match up with actual points; for example, the point (0,5) will not be on the y-axis, it is shifted over.

Here is the code for my graph:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{figure}[ht]
\centering

\begin{tikzpicture}[scale=1.5,line width=1pt]

\begin{axis}[
color= black,
xmin=0, 
xmax=19.5, 
ymin=0, 
ymax=19.5, 
axis equal, 
axis x line=left,
axis y line=left,
disabledatascaling,
xticklabels={}, 
yticklabels={},
font=\scriptsize,
ticks=none,
extra x ticks=0,
extra y ticks=0,
]

\draw (0,5) -- (10,0);

\end{axis}

\end{tikzpicture}
\end{figure}
\end{document}

My issue is not with creating a graph; I only put a simple line to demonstrate that the points are not lining up.

Does anyone know why this is happening? I cannot figure it out at all. Help would be much appreciated!

1
  • 2
    axis y line=middle Sep 6, 2018 at 4:01

2 Answers 2

4

As David already pointed out in his answer this is because of the axis equal option which changes the axis limits to fit the specified width and height of the axis.

Another alternative would be to use the axis equal image key, which changes either the width or the height.

If you always want to be sure that the axis lines are drawn at the zero coordinates, you should use axis lines=middle instead of axis lines=left, as Henri Menke already stated in the comment below the question.

(I also made some other comments to your code in my answer code, which you should have a look at. Just in case you weren't aware of this stuff.)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
%    % (use this `compat' level or higher so by default TikZ coordinates use
%    %  `axis cs:'. See comments below for more details.)
%    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=0,
        xmax=19.5,
        ymin=0,
        ymax=19.5,
        % ---------------------------------------------------------------------
        % replace `axis equal' with `axis equal image'
%        axis equal,
        axis equal image,
        % ---------------------------------------------------------------------
%        % (if you want to be sure that the axis lines are drawn at the
%        %  zero coordinates, than you should use `axis lines=middle'.
%        %  Please note that then by default the zero ticklabels are not
%        %  drawn, because in general these would be written on "the other"
%        %  axis line. To make them appear nonetheless you could add
%        %  `hide obscured x ticks=false' (and similar for the y axis), as
%        %  are commented below the next commented option line.)
        axis lines=left,
%            axis lines=middle,
%            hide obscured x ticks=false,
%            hide obscured y ticks=false,
%        % (if you comment `disabledatascaling' you also don't get the desired
%        %  result, because with the default `compat' level TikZ coordinates
%        %  by default don't use the axis coordinate system (`axis cs:'), but
%        %  the tikzpicture coordinate system (`cs:'). ...
%        disabledatascaling,
    ]
        % (... To overcome this issue you could either prepend all TikZ
        %  coordinates by `axis cs:' or use a `compat' value of 1.11 or higher)
        \draw [red,thick] (        0,5) -- (        10,0);
        \draw [green]     (axis cs:0,5) -- (axis cs:10,0);
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • Thank you so much! I will play around with this more, but you are right, I do not know about all of the small details, and I have a lot more control over how my picture looks now! I appreciate the help so much.
    – akenny430
    Sep 12, 2018 at 19:13
  • 2
    Glad that I could help. If you like my answer and it was helpful, please consider upvoting it (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓). Thank you. Sep 12, 2018 at 20:47
3

The problem is with the axis equal line. axis equal changes the axis limits automatically in order to make the plot fit in the specified height and width. You can see that it's changing the x-axis by commenting out the ticks=none line - the 0 tick mark is not at the y axis.

I recommend removing the axis equal line.

You must log in to answer this question.

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