6

I'm trying to draw an ellipse inside a semi-logarithmic pgfplots axis. The ellipse axes should be parallel to the coordinate axes, but depending on the radii I choose, the ellipse appears rotated.

Here's a MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\pgfplotsset{width=7cm}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}[ymode=log]
  \addplot coordinates{
    (0,10) (1,300) (2,3347) (3,5000)
  };
  \draw 
  (axis cs:1,300) ellipse [
    x radius = 1, y radius = 10];   
  \end{axis}
 \end{tikzpicture}
\end{document}

which produces

screenshot of pgfplots output

How can I align the ellipse with the coordinate axes, so that they are parallel to the ellipse axes?

7
  • Which version of pgfplots do you have?
    – percusse
    Commented Feb 17, 2015 at 22:41
  • @percusse Log file says document has been generated with the most recent feature set (\pgfplotsset{compat=1.9}. So version 1.9, I guess.
    – Deve
    Commented Feb 17, 2015 at 23:16
  • Can you update to the newest version (currently 1.12 but 1.11 is also fine)?
    – percusse
    Commented Feb 17, 2015 at 23:27
  • @percusse With a local installation, maybe. I can try tomorrow.
    – Deve
    Commented Feb 17, 2015 at 23:31
  • 2
    Adding rotate=-45 will get the horizontal ellipse, but that only works for your particular radii. found by trial and error. I know it's not a good solution, but if you need it now it will get you by.
    – Bill N
    Commented Feb 18, 2015 at 0:45

1 Answer 1

5

As a rule, I try to only do normal tikz AFTER \end{axis}. Instead I save coordinates for use later.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\pgfplotsset{width=7cm}

\newlength{\rx}
\newlength{\ry}

\begin{document}
 \begin{tikzpicture}
  \begin{axis}[ymode=log]
  \addplot coordinates{
    (0,10) (1,300) (2,3347) (3,5000)
  };
  \coordinate (Center) at (axis cs:1,300);
  \coordinate (Radius) at (axis cs:2,3000);% x+1, y*10 relative to Center
  \end{axis}
  \pgfextractx{\rx}{\pgfpointdiff{\pgfpointanchor{Radius}{center}}{\pgfpointanchor{Center}{center}}}%
  \pgfextracty{\ry}{\pgfpointdiff{\pgfpointanchor{Radius}{center}}{\pgfpointanchor{Center}{center}}}%
  \draw (Center) ellipse [x radius = \rx, y radius = \ry]; 
 \end{tikzpicture}
\end{document}

ellipse

You must log in to answer this question.

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