在 pgfplots 中强制将绘图标记设置为“轴在顶部”

在 pgfplots 中强制将绘图标记设置为“轴在顶部”

pgfplots 手册说,axis on top=true如果使用 plotmarks,这不会影响绘图,因为这通常没有意义,我同意这一点。但是,在这种情况下,我希望轴和刻度标签都位于绘图标记之上;有什么方法可以强制执行此操作,或者有什么可用的解决方法吗?

目前,我的输出如附图所示,

在此处输入图片描述

使用以下选项

\begin{axis}[%
axis on top=true,
view={0}{90},
axis y line = center,
axis x line = center,
xmin=-2,xmax=2,
ymin=-2,ymax=2,
xtick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
xticklabels={$-2c$,$-1.5c$,$-c$,$-0.5c$,$0$,$0.5c$,$c$,$1.5c$,$2c$},
ytick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
yticklabels={$-2d$,$-1.5d$,$-d$,$-0.5d$,$0$,$0.5d$,$d$,$1.5d$,$2d$},
width=4in,
height=3in,
scale only axis,
xlabel={$\phi_1$},
ylabel={$\phi_2$}]

MWE 代码:

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\input{sn2.tikz}

\end{document}

sn2.tikz 可以在以下链接中找到

链接至 sn2.tikz:sn2.tikz

答案1

因为您要绘制相当多的点,所以我会将绘图部分和装饰轴部分分开。我已删除与绘图轴相关的所有内容,并在其上定义了一个新的。还将颜色更改为黄色,以显示轴确实位于标记上方。

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}

%Data plot 
\begin{axis}[xmin=-2,xmax=2,ymin=-2,ymax=2,axis lines = none]
\addplot [color=yellow,only marks,mark=x,mark options={solid}]
    coordinates{(0.52,-0.15) (0.52,-0.16)(0.52,-0.17)(0.52,-0.19)(0.52,-0.2)};
\end{axis}

% Dummy axis
\begin{axis}[%
axis on top=true,
axis y line = center,
axis x line = center,
xmin=-2,xmax=2,
ymin=-2,ymax=2,
xtick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
xticklabels={$-2c$,$-1.5c$,$-c$,$-0.5c$,$0$,$0.5c$,$c$,$1.5c$,$2c$},
ytick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
yticklabels={$-2d$,$-1.5d$,$-d$,$-0.5d$,$0$,$0.5d$,$d$,$1.5d$,$2d$},
xlabel={$\phi_1$},
ylabel={$\phi_2$}]
]
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您可以使用tikz/pgfplots 高级 z 顺序轴/网格定义新样式axis line on top。这样做的好处是您不必手动保持两个轴的选项同步。

\documentclass[12pt]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother

\pgfplotsset{axis line on top/.style={
  axis line style=transparent,
  ticklabel style=transparent,
  tick style=transparent,
  axis on top=false,
  after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}


\begin{document}

\begin{tikzpicture}

%Data plot 
\begin{axis}[xmin=-2,xmax=2,ymin=-2,ymax=2,axis line on top,
axis y line = center,
axis x line = center,
xmin=-2,xmax=2,
ymin=-2,ymax=2,
xtick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
xticklabels={$-2c$,$-1.5c$,$-c$,$-0.5c$,$0$,$0.5c$,$c$,$1.5c$,$2c$},
ytick={-2,-1.5,-1,-0.5,0,0.5,1,1.5,2},
yticklabels={$-2d$,$-1.5d$,$-d$,$-0.5d$,$0$,$0.5d$,$d$,$1.5d$,$2d$},
xlabel={$\phi_1$},
ylabel={$\phi_2$}]
\addplot [color=yellow,only marks,mark=x,mark options={solid}]
    coordinates{(0.52,-0.15) (0.52,-0.16)(0.52,-0.17)(0.52,-0.19)(0.52,-0.2)};
\end{axis}

% Dummy axis
\begin{axis}[%
]
]
\end{axis}
\end{tikzpicture}

\end{document}

相关内容