PGFplots:x,y 刻度标签与图重叠

PGFplots:x,y 刻度标签与图重叠

我正在尝试使用 pgfplots 绘制两个框。但是,刻度标签与图本身重叠,这很烦人:

在此处输入图片描述

理想情况下,我希望它们位于红色框的顶部,而 k_2^- + \epsilon_2 位于绿色框的顶部。其他两个 xlabel 也类似。源代码如下:

\documentclass[border=1pt, 11pt]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\usepackage{tikz}
\usetikzlibrary{decorations.text}
\usetikzlibrary{arrows, decorations.markings}
\usetikzlibrary[shapes,arrows.meta,bending,shapes.geometric]
 
\begin{document}
\LARGE
\begin{tikzpicture} 
    \makeatletter
\tikzset{
    nomorepostaction/.code=\makeatletter\let\tikz@postactions\pgfutil@empty, % From https://tex.stackexchange.com/questions/3184/applying-a-postaction-to-every-path-in-tikz/5354#5354
    my axis/.style={
        postaction={
            decoration={
                markings,
                mark=at position 1 with {
                    % \arrow[ultra thick]{latex}
                }
            },
            decorate,
            nomorepostaction
        },
        semithick,
        ->, 
        shorten >=0pt,
        >={Stealth[round]},
        every path/.append style=my axis  % this is necessary so it works both with "axis lines=left" and "axis lines=middle"
    }
}
\makeatother

\begin{axis}[
    axis lines = center,
    axis line style = my axis,
    xmin=-1, 
    xmax=1,
    ymin=-0.8, 
    ymax=0.8,
    xlabel = \(x\), 
    xlabel style={xshift=1.5em, yshift=-.75em},
    ylabel = \(y\), 
    ylabel style={rotate=0, xshift=0.25em, yshift=0.25em},
    xtick={ 0, 1/2.2, 1/2+0.4}, 
    xticklabels={ 0, $k_1^-$, $k_1^-+\epsilon_1$},
    ytick={  1/2.5, 2/3},
    yticklabels={ $k_2^-$, $k_2^- + \epsilon_2$},
    xticklabel pos=left,
    yticklabel pos=left,
    width=10cm, height=11cm,
    semithick,
    axis on top
]
\addplot[sharp plot, draw=green, fill=green!20!white, 
semithick] coordinates
      {(-1/2-0.4, 2/3) 
      (1/2+0.4,  2/3) 
      (1/2+0.4, - 2/3)
      (-1/2-0.4,- 2/3)
      (-1/2-0.4,  2/3)};
    %   \closedcycle;
\addplot[sharp plot, draw=magenta, fill=magenta!20!white, semithick] coordinates
      {(-1/2.2, 1/2.5) 
      (1/2.2, 1/2.5) 
      (1/2.2, -1/2.5)
      (-1/2.2,-1/2.5)
      (-1/2.2, 1/2.5)};
 
\node at (132.5, 1125) (label2) {$\mathcal{P}^-$};
\node at (175,   1375) (label2) {$\mathcal{P}^-_{\epsilon}$};

\end{axis}
\end{tikzpicture}   
\end{document}

此外,我以一种不成熟的方式打印了标签 P 和 P_{\epsilon}(在我看来),因为出于某种原因,\node 命令的 (x,y) 坐标与图的坐标不同。我也尝试使用 \draw 命令,但也有同样的问题。

我将非常感激对这些问题的任何帮助!

答案1

如果在环境中使用compat高于 的坐标1.11,则axis默认坐标将是绘图轴坐标。否则,您需要明确使用axis cs:\draw命令\node

我删除了我认为不需要的东西(你确定吗\LARGE?)并且尽可能使用相对定位而不是硬距离。

\documentclass[border=1pt, 11pt]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}

\pgfplotsset{compat=1.18}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,bending}

\begin{document}
\LARGE
\begin{tikzpicture}

\begin{axis}[
    axis lines = center,
    xmin=-1,
    xmax=1,
    ymin=-0.8,
    ymax=0.8,
    xlabel = \(x\),
    xlabel style={right},
    ylabel = \(y\),
    ylabel style={rotate=0, right},
    xtick={ 0, 1/2.2, 1/2+0.4},
    xticklabels={ 0, $k_1^-$, $k_1^-+\epsilon_1$},
    ytick={  1/2.5, 2/3},
    yticklabels={ $k_2^-$, $k_2^- + \epsilon_2$},
    y tick label style = {font=\normalsize, above left},
    x tick label style = {font=\normalsize, below right},
    width=10cm, height=11cm,
    semithick,
    axis on top
]
\addplot[sharp plot, draw=green, fill=green!20!white,
semithick] coordinates
      {(-1/2-0.4, 2/3)
      (1/2+0.4,  2/3)
      (1/2+0.4, - 2/3)
      (-1/2-0.4,- 2/3)
      (-1/2-0.4,  2/3)};
    %   \closedcycle;
\addplot[sharp plot, draw=magenta, fill=magenta!20!white, semithick] coordinates
      {(-1/2.2, 1/2.5)
      (1/2.2, 1/2.5)
      (1/2.2, -1/2.5)
      (-1/2.2,-1/2.5)
      (-1/2.2, 1/2.5)};

\node [below left] at (1/2.2,1/2.5) (label1) {$\mathcal{P}^-$};
\node [below left] at (1/2+0.4,2/3) (label2) {$\mathcal{P}^-_{\epsilon}$};

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容