PGFPlots 在修剪轴、填充之间和错误标记之间的错误

PGFPlots 在修剪轴、填充之间和错误标记之间的错误

使用 PGFPlots 绘制带有误差线的数据点,除了使用函数fill between以及 TikZtrim axis left, trim axis right键之外,我还遇到了一个奇怪的错误,其中错误标记(红色)与误差线分离,并且似乎是随机放置的(更改蓝色框的坐标也会更改红色标记的位置)。我该如何修复此行为?

下面是一个最小的工作示例:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}[trim axis left, trim axis right]
        
    \pgfplotsset{
        plotStyle/.style={
            color=black,
            only marks,
            mark=square*,
            error bars/.cd,
            y dir=both,
            y fixed=1,
            error bar style={very thick},
            error mark options={very thick, mark size=2pt,rotate=90, red}
        },
    }

    \begin{axis}
    
    \addplot [name path = a, draw = none] coordinates {
        (1, 0)
        (1, 2)
    };
    \addplot [name path = b, draw = none] coordinates {
        (2, 0)
        (2, 2)
    };
    \addplot[blue] fill between [of=a and b];
 
    \addplot[plotStyle] coordinates {
        (0, 0)
    };
    \end{axis}
    \end{tikzpicture}
\end{document}

注释掉[trim axis left, trim axis right]\addplot[blue] fill between [of=a and b];将使错误标记出现在正确的位置,但正如上面所示,编译出了以下图表:

该图片显示了带有错误标记的错误

答案1

我不知道问题的原因是什么,但这与 tikzpicture 嵌套在绘制错误栏标记的节点中有关。这是一个解决方法 - 我不能说它是否在所有情况下都有效,或者它是否会以其他方式中断:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[trim axis right]
\begin{axis}
\addplot[name path=a, draw=none] coordinates {(1.5, 1) (1.5, 2)}; \addplot[name path=b, draw=none] coordinates {(2, 1) (2, 2)}; \addplot[blue] fill between [of=a and b];
%% Problematic error bar
\addplot[red,error bars/y dir=both, error bars/y fixed=1] coordinates {(0,1)};
%%% Code from pfgplots.code.tex used to draw half errorbar (with color and coordinates inserted)
\pgfkeysgetvalue{/pgfplots/error bars/error mark}{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}{\pgfplotserrorbarsmarkopts}%
\draw[cyan, yshift={\pgfplotbarshift}] (0.2,1) -- (0.2,2) node[pos=1,sloped,allow upside down] {%
                \expandafter\tikz\expandafter[\pgfplotserrorbarsmarkopts]{%
                    \expandafter\pgfuseplotmark\expandafter{\pgfplotserrorbarsmark}%
                    \pgfusepath{stroke}}%
            };
%% draw without problems:
\draw[green] (0.4,1) -- (0.4,2) node[pos=1, sloped, allow upside down] {$\vert$};
%% minimal draw with problems:
\draw[orange] (0.6,1) -- (0.6,2) node[pos=1, sloped, allow upside down] {\tikz{$\vert$}};
%% Naive try that actually worked
\draw[pink] (0.8,1) -- (0.8,2) node[pos=1, sloped, allow upside down] {\tikz[trim left=0pt, trim right=0pt]{$\vert$}};
%% Working error bar
\addplot[red, error bars/y dir=both, error bars/y fixed=1, 
  error bars/error mark options={rotate=90, trim left=0pt, trim right=0pt}] coordinates {(1,1)};
\end{axis}
\end{tikzpicture}
\end{document}

带有多条误差线的图表,其中有些误差线的两端分离

相关内容