# Drawing Shapes on Graphs

I am trying to draw a regular pentagon on a graph (I need to add more stuff to it, so having the axis is desirable), but when I try to draw the two together they do not line up. Here is my code:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgflibrary{shapes.geometric}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}[scale=1.25,line width=1pt]
\begin{axis}[
color= black,
xmin=-9.9,
xmax=9.9,
ymin=-9.9,
ymax=9.9,
axis equal image,
axis lines=middle,
xticklabels={},
yticklabels={},
font=\scriptsize,
xlabel = $x$,
ylabel = $y$,
inner axis line style={stealth-stealth},
xtick distance=1,
ytick distance=1,
legend style={draw=none},
ticks=none,
]

\node[black] at (0,0) {and not here?};

\end{axis}

\foreach \a in {5}{
\node [regular polygon, regular polygon sides=\a, minimum size=5cm,
draw] at (0,0) (A) {};
\foreach \i in {1,...,\a}
{%
\node [label=90+72*(\i-1):\i, inner sep=1pt] at (A.corner \i) {};
}
}

\node[black] at (0,0) {why is it here?};

\end{tikzpicture}

\caption{A regular pentagon}
\label{regpent}
\end{figure}

\end{document}


Here is the image that is produced: I would like to have the center of the pentagon line up with the origin of the axis. My guess is that the coordinate system is different in the axis environment and outside it, but I'm not sure.

as supplement to @Ignasi answer. with simplified axis options and with labeling of pentagon corners:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgflibrary{shapes.geometric}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}[
lbl/.style = {fill=white, inner sep=2pt, font=\footnotesize, pos=1.1}
]
\def\a{5}
\begin{axis}[
axis lines=middle,
ticks=none,
xlabel = $x$,
ylabel = $y$,
xmin=-10, xmax=10,
ymin=-10, ymax=10,
]
% poygon
\node[regular polygon,
regular polygon sides=\a, draw,
minimum size=22mm] (A) at (0,0) {i'm in center};
\end{axis}
% corners labels
\foreach \i in {1,...,\a}%
{
\path (A.center) -- node [lbl] {\i} (A.corner \i) ;
}

\end{tikzpicture}

\caption{A regular pentagon}
\label{regpent}
\end{figure}

\end{document} • For me the label positioning breaks when I add scale=1.25 to the tikzpicture as the OP had. Putting the scale on the axes instead of the tikzpicture fixes this (but of course doesn't scale the stuff outside the axes). – Emma Jan 23 '19 at 14:14
• @Emma, in pgfplots you can define image size width=..., height=... accordingly, so adding scale is not necessary. using scale many times make problems ... – Zarko Jan 23 '19 at 14:23
• Well, yes, clearly scale causes problems in this case. There are other ways to resize the axes but they wouldn't change the stuff outside the axes. A workaround is to scale the axes and then separately scale any other content outside them. But I'm still surprised that scaling the whole tikzpicture causes coordinates named inside the axes to move elsewhere when referenced outside them. – Emma Jan 24 '19 at 5:47
• @Emma, scaling apparently doesn't work as you expected. So far op were not complain about solution, so you open (here in comments) new problem, not addressed in question. To clarify, how it works is better to ask new question (comments for this are to limited) or read pgfplots and pgf & tikz documentation. – Zarko Jan 24 '19 at 10:48

Did you try to draw the pentagon inside axis?

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgflibrary{shapes.geometric}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}[scale=1.25,line width=1pt]
\begin{axis}[
color= black,
xmin=-9.9,
xmax=9.9,
ymin=-9.9,
ymax=9.9,
axis equal image,
axis lines=middle,
xticklabels={},
yticklabels={},
font=\scriptsize,
xlabel = $x$,
ylabel = $y$,
inner axis line style={stealth-stealth},
xtick distance=1,
ytick distance=1,
legend style={draw=none},
ticks=none,
]

\node[black] at (0,0) {and not here?};

\node [regular polygon, regular polygon sides=5, minimum size=5cm,
draw] at (0,0) (A) {};

\node[black] at (0,0) {why is it here?};

\end{axis}

\end{tikzpicture}

\caption{A regular pentagon}
\label{regpent}
\end{figure}

\end{document}


Note: I know there is some problem with foreach inside axis but I don't know how to solve it now. • Yes, that was my issue and the original reason I did not include the pentagon inside the axis environment to begin with. – Aiden Kenny Jan 16 '19 at 15:02
• Nice answer! Try \pgfplotsinvokeforeach{1,...,5}% { \node[anchor=-90+72*(#1-1)] at (A.corner #1) {#1} ; } – user121799 Jan 17 '19 at 2:08