# Drawing a vector from transformed origin with tikzpicture and pgfplots

I'm trying to draw a vector in Latex using tikzpicture and pgfsplot in my beamer presentation. Unfortunately, I don't get the desired output. The following code

\documentclass{beamer}
\usetheme{metropolis} % Use metropolis theme
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{figure}[!htb]
\vspace{-0.5cm}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=-6.067, xmax=6.433,
ymin=-1.3,ymax=2.2,
scatter/classes={a={mark=o,draw=black}},
xlabel={$x$},
ylabel={$y$},
scale=0.9
]

\addplot[scatter,only marks,mark size = 1pt,fill = red,scatter src=explicit symbolic]
table {
% some points go here...
};

\end{axis}
\draw[|->, black](0,0) -- (3,1);
\end{tikzpicture}
\end{figure}
\end{document}


produces the following output: However, this is not what I want. What I would like to have is a vector from the origin (0,0) to the point (3,1) while keeping the limits of my axes as they are.

Any help is greatly appreciated.

• \draw[|->, black](axis cs:0,0) -- (axis cs:3,1); or even easier, add \pgfplotsset{compat=newest} to your preamble. – Henri Menke Jun 11 '18 at 8:27
• Thank you very much. That solved my problem. However, it has to be noted that your statement has to be within the \begin{axis]... \end{axis} scope as I ended up with errors outside of this scope. A small follow-up question: How would I go about extending the vector such that it extends from one axis to the other? – Hagbard Jun 11 '18 at 8:31

like this:

\documentclass{beamer}
\usetheme{metropolis} % Use metropolis theme
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{frame}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=-6.067, xmax=6.433,
ymin=-1.3,ymax=2.2,
scatter/classes={a={mark=o,draw=black}},
xlabel={$x$},
ylabel={$y$},
scale=0.9
]
\addplot[scatter,only marks,mark size = 1pt,fill = red,scatter src=explicit symbolic]
coordinates { (-6,1) (0,2) (6,1)
% some points go here...
};