# Rotated ellipse problem with center

Hallo TeX/Tikz community,

I have data from a random bivariate normal distribution (not included in the code) and I want to add an ellipse, a circle and a coordinate system rotated by 45°. I have three problems (It is not necessary to give a solution to all the problems) that I was not able to solve:

1. If I enter \draw[rotate around={45:(500,500)},black](500,500) ellipse (2cm and 1 cm); The resulting ellipse is somewhere in the left bottom corner. So I thought that the coordinates that need to be entered are relative to the bottom corner of the plot area. That is why I replaced (0,0) by (500,500). Which looks pretty close to the real (0,0) of my coordinate system but only close. What is going wrong here?

2. I also would like to have slender arrow heads with triangle shape for the end of the lines. But I could not figure out how this can be done with tikz.

3. The last problem is concerning the data points despite the fact that I want them in the background. After adding them to the plot they seem to be always overlapping the other drawings. I tried to put them before the other drawings and after them in the code, but that didn't change anything.

I would be glad if someone could help me out with my questions.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x_1$},
ylabel={$x_2$},
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
tick pos = left]

% Draw ellipse
\draw[rotate around={45:(500,500)},black](500,500) ellipse (2cm and 1 cm);
\draw[black,line width=1pt,red,dashed](500,500) ellipse (2cm and 2 cm);
\draw[black,line width =1pt] (500,500) -- (800,800);
\draw[rotate around={90:(500,500)},black,line width =1pt] (500,500) -- (800,800);

\end{axis}
\end{tikzpicture}
\end{document}

• You have the right diagnostics for question 1. you should look for the keyword coordinate system in the pgfplots manual (axis cs and so on...) – marsupilam Aug 23 '17 at 13:16

By default \draw and the like does not use the coordinate system of the axis, as marsupilam says, to make sure it does use the axis coordinates, you can use (axis cs:x,y), instead of just (x,y). But if you have \pgfplotsset{compat=1.11} or newer, then axis cs is default, so you don't need to specify it.

For the arrow tip, look at the description of the arrows.meta library in the manual for pgf/TikZ.

Marks are plotted on top by default, but you can circumvent that with clip mode=individual, cf. Line in front of marks

Finally, note that while you draw a circle, your axis doesn't have the same scale on both x- and y-axis, so in the axis coordinate system it's not actually a circle (you've drawn circle and ellipse using explicit lengths, 2cm and 1cm, not in axis units). To make sure the scale is correct, you can plot the circle and ellipse as well. You can also add axis equal to the axis options, as in the code below, so ensure the same scale.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{arrows.meta}
\tikzset{
% define a new arrow tip
NarrowTri/.tip={Triangle[length=4pt,width=1.5pt]}
}

% If you uncomment the following, "axis cs:" isn't needed in the rotate around.
%\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x_1$},
ylabel={$x_2$},
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
tick pos = left,
% equal scale on x- and y-axis
axis equal,
% https://tex.stackexchange.com/questions/84650/line-in-front-of-marks
clip mode=individual
]

% plot points

% draw circle
% draw ellipse
% draw axis
\addplot [NarrowTri-NarrowTri] coordinates {({-2*sqrt(2)},{2*sqrt(2)}) (0,0) ({2*sqrt(2)},{2*sqrt(2)})};

\end{axis}
\end{tikzpicture}
\end{document}


To see what I meant by the scaling, consider the following image and the code that produced it:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=ax1,
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
title={without \texttt{axis equal}}
]

% plot circle
% draw circle
\draw[black,line width=1pt,red,dashed](0,0) ellipse (2cm and 2 cm);
\end{axis}
\begin{axis}[
at={(ax1.south east)},
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
axis equal,
ytick pos=right,
title={with \texttt{axis equal}}
]

% plot circle
% draw circle
\draw[black,line width=1pt,red,dashed](0,0) ellipse (2cm and 2 cm);
\end{axis}
\end{tikzpicture}
\end{document}

• Very good answer ! – marsupilam Aug 23 '17 at 13:49
• +1: Thank you, that is answering all my questions. You also selected exactly the arrowheads that I wanted. The strange thing about this is, that I didn't have the same problems in another tikz plot. I could use absolute coordinates without anything additional. – MrYouMath Aug 23 '17 at 13:52
• @MrYouMath TikZ, or pgfplots? The problem here occurred because you were using standard TikZ-macros (\draw) inside an axis environment from pgfplots, i.e. mixing the two. – Torbjørn T. Aug 24 '17 at 8:00

You are using an old version of pgfplots if you don't set \pgfplotsset{compat=1.15}.

A lot of these inconveniences with coordinate systems are solved by saner defaults in recent versions.

## The code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[>=stealth]
\begin{axis}
[
xlabel={$x_1$},
ylabel={$x_2$},
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
tick pos = left,
]

% Draw ellipse
\begin{scope}[rotate around={45:(0,0)},myVeryThick/.style={line width=1pt}]
\draw (0,0) ellipse (2cm and 1 cm);
\draw[myVeryThick,red,dashed] (0,0) circle (2cm);
\draw[myVeryThick,->] (0,0) -- (0,3cm);
\draw[myVeryThick,->] (0,0) -- (3cm,0);
\end{scope}

\end{axis}
\end{tikzpicture}
\end{document}


### Proof the tikz part is now drawn at the right time

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15} % current pgfplots version
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[>=stealth]
\begin{axis}
[
xlabel={$x_1$},
ylabel={$x_2$},
xmin = -5,
xmax = 5,
ymin = -5,
ymax = 5,
tick pos = left,
]

\addplot [blue, very thick] {x} ;

% Draw ellipse
\begin{scope}[rotate around={45:(0,0)},myVeryThick/.style={line width=1pt}]
\fill[blue!10] (0,0) circle (2cm);
\draw (0,0) ellipse (2cm and 1 cm);
\draw[myVeryThick,red,dashed] (0,0) circle (2cm);
\draw[myVeryThick,->] (0,0) -- (0,3cm);
\draw[myVeryThick,->] (0,0) -- (3cm,0);
\end{scope}

\addplot [green!50!black, very thick] {x+1} ;

\end{axis}
\end{tikzpicture}
\end{document}

• Did the scope environment allow absolute coordinates? And what does stealth result in? – MrYouMath Aug 23 '17 at 13:30
• >=stealth sets the default arrow tips style to stealth. It is the style that appears here. – marsupilam Aug 23 '17 at 13:36
• I get what scope is doing, but I still do not understand how the coordinates changes. And your code does not give me the right result. It only displays one large plot which is partially cropped. Also does the arrow of the x-axis not appear to be parallel with the blue line. – MrYouMath Aug 23 '17 at 13:39
• Try replacing the first line with \documentclass{article}. Your question about coordinate systems is explained in the pgfplots manual. – marsupilam Aug 23 '17 at 13:42