将刻度标签放置在绘图内容顶部

将刻度标签放置在绘图内容顶部

我想出了一个巧妙的方法,通过将刻度刻度标签移到轴窗口中来避免堆叠图的刻度刻度标签和轴边界重叠,但不幸的是,线条画在了它们上面。我试着摆弄这个问题,但却无果。

无论是通过强制最后绘制刻度标签还是其他方法,我如何确保刻度标签不被任何东西遮挡?

梅威瑟: 在此处输入图片描述

\documentclass[crop, tikz]{standalone}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{mathrsfs}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}

\usepackage{xcolor}
\definecolor{tab10_1}{rgb}{0.1216, 0.4667, 0.7059}
\definecolor{tab10_2}{rgb}{1.0, 0.498, 0.0549}
\definecolor{tab10_3}{rgb}{0.1725, 0.6275, 0.1725}
\definecolor{tab10_4}{rgb}{0.8392, 0.1529, 0.1569}
\definecolor{tab10_5}{rgb}{0.5804, 0.4039, 0.7412}
\definecolor{tab10_6}{rgb}{0.549, 0.3373, 0.2941}
\definecolor{tab10_7}{rgb}{0.8902, 0.4667, 0.7608}
\definecolor{tab10_8}{rgb}{0.498, 0.498, 0.498}
\definecolor{tab10_9}{rgb}{0.7373, 0.7412, 0.1333}
\definecolor{tab10_10}{rgb}{0.0902, 0.7451, 0.8118}
\pgfplotscreateplotcyclelist{tab10}{
    {tab10_1},
    {tab10_2},
    {tab10_3},
    {tab10_4},
    {tab10_5},
    {tab10_6},
    {tab10_7},
    {tab10_8},
    {tab10_9},
    {tab10_10}
}

\newlength{\figureheight}
\newlength{\figurewidth}

\pgfplotsset{
    every axis/.append style = {
        line join = round,
        line cap = round,
        cycle list name = tab10,
        grid = major,
        grid style = {
            dashed,
            color = black!50
        },
    },
    every y tick scale label/.style={
        % This looks great -- until the line gets drawn on top!
        at = {(0, 1)},
        xshift = 0pt,
        anchor = north west,
        outer sep = 0pt,
        inner sep = 0.2em,
        fill = white,
        draw = black
    },
    ytick scale label code/.code={
        % The default code. Maybe I could tweak this to my needs somehow?
        $\pgfkeysvalueof{/pgfplots/tick scale binop} 10^{#1}$
    }
}

\begin{document}
\begin{tikzpicture}
\pgfmathsetlength{\figureheight}{4.5cm}
\pgfmathsetlength{\figurewidth}{12cm}
\begin{groupplot}[
    group style={group size={1 by 2},
    vertical sep={\figureheight/15}},
    height={\figureheight},
    width={\figurewidth}, 
    xtick={0.0, 0.5, 1.0}
]
    \nextgroupplot[
        ylabel={$\bm{x}$ (n.d.)},
        legend columns={-1},
        legend pos={north east},
        legend entries={$x_1$, $x_2$, $x_3$},
        xticklabel={\empty},
        title={Labels Occluded By Lines}
    ]
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  1.0e-10  \\
            1.0  1.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.0e-10  \\
            1.0  0.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.5e-10  \\
            1.0  0.5e-10  \\
        }
        ;
    \nextgroupplot[
        ylabel={$\bm{x}^\prime$ (n.d.)},
        legend columns={-1},
        legend pos={north east},
        legend entries={{$x^\prime_1$, $x^\prime_2$, $x^\prime_3$}},
        xlabel={Common abscissa axis}
    ]
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  1.0e-10  \\
            1.0  1.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.0e-10  \\
            1.0  0.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.5e-10  \\
            1.0  0.5e-10  \\
        }
        ;
\end{groupplot}
\end{tikzpicture}
\end{document}

答案1

axis on top如果您不介意网格出现在图表上方,您可以简单地使用该选项。

\pgfplotsset{axis on top}

如果你希望网格位于曲线下方,轴标签位于上方,则应使用图层来精确组织不同的元素(参见pgfplots手册第 4.27 节,或Tikz 轴边框在顶部,网格在下方)。看来 y 刻度标签在层中main。定义 deplot层,在main层下方:

\pgfplotsset{
    /pgfplots/layers/my layers/.define layer set={
      axis background,
      axis grid,
      axis ticks,
      pre main,
      plot,
      main,
      axis lines,
      axis tick labels,
      axis descriptions,
      axis foreground
    }{/pgfplots/layers/standard},
    set layers=my layers,
}

并在该图层上绘制曲线:

 every axis plot/.style={on layer=plots}

完整代码:

\documentclass[crop, tikz]{standalone}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{mathrsfs}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}

\usepackage{xcolor}
\definecolor{tab10_1}{rgb}{0.1216, 0.4667, 0.7059}
\definecolor{tab10_2}{rgb}{1.0, 0.498, 0.0549}
\definecolor{tab10_3}{rgb}{0.1725, 0.6275, 0.1725}
\definecolor{tab10_4}{rgb}{0.8392, 0.1529, 0.1569}
\definecolor{tab10_5}{rgb}{0.5804, 0.4039, 0.7412}
\definecolor{tab10_6}{rgb}{0.549, 0.3373, 0.2941}
\definecolor{tab10_7}{rgb}{0.8902, 0.4667, 0.7608}
\definecolor{tab10_8}{rgb}{0.498, 0.498, 0.498}
\definecolor{tab10_9}{rgb}{0.7373, 0.7412, 0.1333}
\definecolor{tab10_10}{rgb}{0.0902, 0.7451, 0.8118}
\pgfplotscreateplotcyclelist{tab10}{
    {tab10_1},
    {tab10_2},
    {tab10_3},
    {tab10_4},
    {tab10_5},
    {tab10_6},
    {tab10_7},
    {tab10_8},
    {tab10_9},
    {tab10_10}
}

\newlength{\figureheight}
\newlength{\figurewidth}

\pgfplotsset{
    every axis/.append style = {
        line join = round,
        line cap = round,
        cycle list name = tab10,
        grid = major,
        grid style = {
            dashed,
            color = black!50
        },
    },
    every y tick scale label/.style={
        % This looks great -- until the line gets drawn on top!
        at = {(0, 1)},
        xshift = 0pt,
        anchor = north west,
        outer sep = 0pt,
        inner sep = 0.2em,
        fill = white,
        draw = black
    },
    ytick scale label code/.code={
        % The default code. Maybe I could tweak this to my needs somehow?
        $\pgfkeysvalueof{/pgfplots/tick scale binop} 10^{#1}$
    },
    /pgfplots/layers/my layers/.define layer set={
      axis background,
      axis grid,
      axis ticks,
      pre main,
      plots,
      main,
      axis lines,
      axis tick labels,
      axis descriptions,
      axis foreground
    }{/pgfplots/layers/standard},
    set layers=my layers,
}

\begin{document}
\begin{tikzpicture}
\pgfmathsetlength{\figureheight}{4.5cm}
\pgfmathsetlength{\figurewidth}{12cm}
\begin{groupplot}[
    group style={group size={1 by 2},
    vertical sep={\figureheight/15}},
    height={\figureheight},
    width={\figurewidth},
    xtick={0.0, 0.5, 1.0},
    every axis plot/.style={on layer=plots}
]
    \nextgroupplot[
        ylabel={$\bm{x}$ (n.d.)},
        legend columns={-1},
        legend pos={north east},
        legend entries={$x_1$, $x_2$, $x_3$},
        xticklabel={\empty},
        title={Labels Occluded By Lines},
    ]
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  1.0e-10  \\
            1.0  1.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.0e-10  \\
            1.0  0.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.5e-10  \\
            1.0  0.5e-10  \\
        }
        ;
    \nextgroupplot[
        ylabel={$\bm{x}^\prime$ (n.d.)},
        legend columns={-1},
        legend pos={north east},
        legend entries={{$x^\prime_1$, $x^\prime_2$, $x^\prime_3$}},
        xlabel={Common abscissa axis}
    ]
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  1.0e-10  \\
            1.0  1.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.0e-10  \\
            1.0  0.0e-10  \\
        }
        ;
    \addplot+[no markers, very thick]
        table[row sep={\\}]
        {
            x  y  \\
            0.0  0.5e-10  \\
            1.0  0.5e-10  \\
        }
        ;
\end{groupplot}
\end{tikzpicture}
\end{document}

图表上方带有标签的图形

相关内容