pgfplots:额外的 x 刻度;刻度长度;影响轴标签位置

pgfplots:额外的 x 刻度;刻度长度;影响轴标签位置
  • 我想要一个extra x tick
  • 在我的“真实”示例中,需要将extra x tick label移动到-y方向(使用major tick length)。
  • pgfplots巧妙地还自动移动了xlabel方向-y
  • 这种通常是可取的行为在我的特殊情况下却是一个问题。
  • 问题:我可以防止 的位置xlabel受到 的影响major tick length吗?我可以不是想要手动使用,xlabel shift = -20pt,因为在我的真实例子中,这仍然会导致细微的差别。

\documentclass{article} 

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

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        axis x line= bottom,
        axis y line= left,
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x +4$},
        extra x ticks = {-5},
        extra x tick style = {
            major tick length = 20pt, % <-- Relevant
            xtick align = outside,    % <-- Relevant
            }
    ]
    % use TeX as calculator:
    \addplot {x^2 - x +4};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        axis x line= bottom,
        axis y line= left,  
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x +4$},
        extra x ticks = {-5},
    ]
    % use TeX as calculator:
    \addplot {x^2 - x +4};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

这是因为您还需要考虑正常的主要刻度长度......

xlabel style为了证明此方法有效,请注释以和开头的两行,extra x ticks并在 PDF 更新时仔细查看。您不应该注意到 xlabel 位置的跳跃。

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
        % store the extra tick length in a variable, so you don't need to repeat yourself
        \pgfmathsetlengthmacro{\ExtraMajorTickLength}{20pt}
    \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x + 4$},
        % ---------------------------------------------------------------------
        % this gives you the "correct" xlabel positioning
        % (the `/2` is needed, because the normal ticks are centered and thus
        %  only half of the tick length is below the x-axis line)
        xlabel style={
            yshift={\ExtraMajorTickLength + \pgfkeysvalueof{/pgfplots/major tick length}/2},
        },
        % ---------------------------------------------------------------------
        extra x ticks={-5},
        extra x tick style={
            major tick length=\ExtraMajorTickLength,
            xtick align=outside,
        },
    ]
        \addplot {x^2 - x + 4};
    \end{axis}
        % (for debugging purposes only to not change the size of the `tikzpicture`
        %  when commenting lines as stated in the text)
        \fill [red] (0,-1.2) circle [radius=1pt];
\end{tikzpicture}
\end{document}

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

相关内容