如何在不改变轴或网格线(pgfplots)的情况下将一组特定的轴刻度更改为“在顶部”?

如何在不改变轴或网格线(pgfplots)的情况下将一组特定的轴刻度更改为“在顶部”?

我画了一个笛卡尔坐标平面pgfplots,并添加了一条线= 1 红色。

not zoomed
not zoomed
example of output
zoomed
zoomed
example of output

轴刻度=1被红线遮住了。


我想

  • 绘制具体= 1 轴打钩在红线之上
  • 没有改变其他任何东西(不是红线,不是轴,不是网格,不是其他刻度,不是轴标签等)例如,红线应该位于轴本身的顶部,并且位于网格线的顶部。

更一般地,最好添加类似

yaxis ticks on top={<list of ticks to be on top>}

对另一个轴也同样如此。可以这样做吗?


\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepackage{xcolor} %here only to make a custom colour


\newcommand{\size}{2}
\colorlet{verylightgray}{lightgray!35} %making a new colour, verylightgray


\pgfplotsset{my style/.append style={axis x line=middle, axis y line=middle, xlabel={$x$}, xlabel style={anchor=north east}, ylabel={$y$}, ylabel style={anchor=north east}, axis equal image, yticklabel style={anchor=south}, grid style={thin, verylightgray}}}
\pgfplotsset{compat=1.17}


\begin{document}

    \begin{tikzpicture}

        \begin{axis}[my style,
            xmin=-\size, xmax=\size, ymin=-\size, ymax=\size,
            xtick={-\size,-\size+1,...,\size}, ytick={-\size,-\size+1,...,\size},
            xticklabels={}, yticklabels={},
            grid=major
        ]

        \draw[thick, red] (axis cs: -3,1) -- (axis cs: 3,1); %here is the red line
        
        \end{axis}

    \end{tikzpicture}

\end{document}

答案1

如果只应在顶部显示特定的勾号,则解决方案如下。 (正如另一个答案下面的评论中所述,应该有一个非常非常好的理由来说明为什么要这样做。)

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{xcolor}
\usepackage{pgfplots}
    \newcommand{\size}{2}
    \colorlet{verylightgray}{lightgray!35} %making a new colour, verylightgray
    \pgfplotsset{
        compat=1.17,
        my style/.append style={
            % -----------------------------------------------------------------
            % added stuff
            set layers,
            extra tick style={
                tick style={
                    % this should do the trick ...
                    /pgfplots/on layer=axis descriptions,
                    % ... but in case it is not "high" enough use this
%                    /pgfplots/on layer=axis foreground,
                },
                yticklabels={},
                grid=none, % <- this prevents new grid lines from being added when special ticks are introduced
            },
            extra y ticks={1},
            % -----------------------------------------------------------------
            axis x line=middle,
            axis y line=middle,
            xlabel={$x$},
            xlabel style={anchor=north east},
            ylabel={$y$},
            ylabel style={anchor=north east},
            axis equal image,
            yticklabel style={anchor=south},
            grid style={thin, verylightgray},
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my style,
        xmin=-\size, xmax=\size, ymin=-\size, ymax=\size,
        xtick={-\size,-\size+1,...,\size}, ytick={-\size,-\size+1,...,\size},
        xticklabels={}, yticklabels={},
        grid=major,
    ]
        \draw [thick, red] (axis cs: -3,1) -- (axis cs: 3,1);
    \end{axis}
\end{tikzpicture}
\end{document}

显示上述代码的放大结果的图像

答案2

那么这就是您根据新要求所寻找的内容吗?

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{xcolor}
\usepackage{pgfplots}
    \newcommand{\size}{2}
    \colorlet{verylightgray}{lightgray!35} %making a new colour, verylightgray
    \pgfplotsset{
        compat=1.17,
        % based on `axis on top`
        /pgfplots/layers/my axis on top/.define layer set={
            axis background,
            axis grid,              % <-- moved here
            pre main,
            main,
%            axis grid,
            axis ticks,
            axis lines,
            axis tick labels,
            axis descriptions,
            axis foreground,
        }{
            /pgfplots/layers/standard
        },
        my style/.append style={
            % set the newly add layer set
            set layers=my axis on top,  % <-- added
            axis x line=middle,
            axis y line=middle,
            xlabel={$x$},
            xlabel style={anchor=north east},
            ylabel={$y$},
            ylabel style={anchor=north east},
            axis equal image,
            yticklabel style={anchor=south},
            grid style={thin, verylightgray},
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my style,
        xmin=-\size, xmax=\size, ymin=-\size, ymax=\size,
        xtick={-\size,-\size+1,...,\size}, ytick={-\size,-\size+1,...,\size},
        xticklabels={}, yticklabels={},
        grid=major,
    ]
        \draw [thick, red] (axis cs: -3,1) -- (axis cs: 3,1);
    \end{axis}
\end{tikzpicture}
\end{document}

图片显示了上述代码的放大结果

答案3

下面我给出了两个类似的解决方案。两者都与层有关。

  • 在“简单”解决方案中,您set layers可以加载并分配您想要使用该on layer功能的图层。
  • 在“稍微复杂一点”的解决方案中,我们根据standard图层集定义一个新的图层集。在其中,我们将axis lines图层移动到图层之后的某个位置main。最后,我们使用 加载该图层集set layers
% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{xcolor}
\usepackage{pgfplots}
    \newcommand{\size}{2}
    \colorlet{verylightgray}{lightgray!35} %making a new colour, verylightgray
    \pgfplotsset{
        compat=1.17,
        /pgfplots/layers/my axis on top/.define layer set={
            axis background,
            axis grid,
            axis ticks,
%            axis lines,
            axis tick labels,
            pre main,
            main,
            axis lines,             % <-- moved here
            axis descriptions,
            axis foreground,
        }{
            /pgfplots/layers/standard
        },
        my style/.append style={
%            % "simple" solution just requires to `set layers`
%            set layers,
            % bit more complicated solution requires to also set the newly add layer set
            set layers=my axis on top,  % <-- added
            axis x line=middle,
            axis y line=middle,
            xlabel={$x$},
            xlabel style={anchor=north east},
            ylabel={$y$},
            ylabel style={anchor=north east},
            axis equal image,
            yticklabel style={anchor=south},
            grid style={thin, verylightgray},
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my style,
        xmin=-\size, xmax=\size, ymin=-\size, ymax=\size,
        xtick={-\size,-\size+1,...,\size}, ytick={-\size,-\size+1,...,\size},
        xticklabels={}, yticklabels={},
        grid=major,
    ]
        % "simple" solution
        \addplot+ [
            orange,
            % state the layer where you want to have the line
            on layer={axis background},
        ] coordinates {
            (\pgfkeysvalueof{/pgfplots/xmin},0.5)
            (\pgfkeysvalueof{/pgfplots/xmax},0.5)
        };

        \draw [thick, red] (axis cs: -3,1) -- (axis cs: 3,1);
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容