在图表上绘制形状

在图表上绘制形状

我正在尝试在图表上绘制一个正五边形(我需要在其中添加更多内容,因此最好有轴),但是当我尝试将两者绘制在一起时,它们并不对齐。这是我的代码:

\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}

生成的图像如下:

在此处输入图片描述

我希望五边形的中心与轴的原点对齐。我猜想坐标系在轴环境和轴外是不同的,但我不确定。

答案1

作为@Ignasi 答案的补充。使用简化的axis选项并标记五边形角:

\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}

在此处输入图片描述

答案2

您尝试过在里面画五边形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}

foreach注意:我知道里面存在一些问题axis,但我现在不知道如何解决。

在此处输入图片描述

相关内容