3

I would like to plot some circles with pgfplots inside a diagram, however, they are always off-centered.

\pgfplotsset{width=7cm, compat=1.12}
\pgfplotsset{compat=1.5.1} 
\begin{tikzpicture}
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-2.5, ymax=2.5,
xtick={-2,-1,0,1,2},
ytick={-2,-1,0,1,2},
grid=major,
]
% standard tikz syntax:
\draw[black] (0,0)
ellipse [
x radius=1, y radius=2];
\draw[red] (0,0)
ellipse [rotate=90,
x radius=1, y radius=2];
\end{axis}
\end{tikzpicture}

According to the pgf-plot documentation, this should give me (http://pgfplots.sourceforge.net/pgfplots.pdf, page 298)

PGF Documentaion

But instead, I get something like this:

My diagram

Could you please show me my mistake?

2
  • Why do you have two different compat?
    – Alenanno
    May 15 '15 at 1:06
  • This is your first question, so Welcome to the site. You can have a look at our starter guide to familiarize yourself further with our format.
    – user11232
    May 15 '15 at 1:17
7

You have used compat two times with \pgfplotsset{compat=1.5.1} at the last. So this prevails over compat=1.12. In older versions of pgfplots the coordinates for drawing should be mentioned as

\draw[black] (axis cs: 0,0) ellipse [...

Note the axis cs:. If you don't use axis cs:, the tikz coordinates are used for which the origin (0,0) lies at the lower left corner and since pgfplots clips the area you get only the first quadrant. Only from version 1.12 onwards one can use (0,0).

Hence, if you have an older version, use axis cs:. On version 1.12, we get this:

\documentclass[border=2]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.12}
%\pgfplotsset{compat=1.5.1}   %% why 2 times?
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-2.5, ymax=2.5,
xtick={-2,-1,0,1,2},
ytick={-2,-1,0,1,2},
grid=major,
]
% standard tikz syntax:
\draw[black] (0,0)
ellipse [
x radius=1, y radius=2];
\draw[red] (0,0)
ellipse [rotate=90,
x radius=1, y radius=2];
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

For the older version 1.5.1, you are loading, this would be

\documentclass[border=2]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.12}
\pgfplotsset{compat=1.5.1}   %% why 2 times?
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2.5, xmax=2.5,
ymin=-2.5, ymax=2.5,
xtick={-2,-1,0,1,2},
ytick={-2,-1,0,1,2},
grid=major,
]
% standard tikz syntax:
\draw[black] (axis cs: 0,0)
ellipse [
x radius=1, y radius=2];
\draw[red] (axis cs: 0,0)
ellipse [rotate=90,
x radius=1, y radius=2];
\end{axis}
\end{tikzpicture}
\end{document}

BTW, you don't need to rotate the ellipse but just interchange the x and y radii like

\draw[red] (axis cs: 0,0)
ellipse [x radius=2, y radius=1];
0
1

You specify compat twice. If you use just

\pgfplotsset{compat=1.12}

it works fine. At some point in its development, PGFPLOTS made draw commands in an axis environment default to the axis cs coordinate system. Using the earlier compat=1.5, \draw uses the generic PGF coordinates, and it looks like PGF axes are drawn at PGF (0,0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.