filling plot region changes error bars of another plot

I have the following plot:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
X Xminus Xplus
1.13 0.1 0.2
1.14 0.02 0.7
1.145 0.23 0.23
}\data
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{document}
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width             = 6.5cm,
enlarge y limits  = {abs=0.5},
axis y line*      = middle,
y axis line style = dashed,
ytick             = \empty,
axis x line*      = bottom
]
\addplot+[mark=none, update limits=false,color = black] coordinates {(1.1, -1) (1.1, \numberofrows-0.5)};
\addplot+[mark=none, update limits=false, dashed, color = black,name path=errmin] coordinates {(1.05, -1) (1.05, \numberofrows-0.5)};
\addplot+[mark=none, update limits=false, dashed, color=black,name path=errmax] coordinates {(1.15, -1) (1.15, \numberofrows-0.5)};
% \addplot+[mark=none, update limits=false, fill=black!10] fill between [of=errmin and errmax];
\addplot+[only marks, blue, thick, mark=*, solid, error bars/.cd,x dir=both, x explicit]
table [x=X,y expr=\coordindex,x error minus=Xminus, x error plus=Xplus]{\data};
\end{axis}
\end{tikzpicture}

\end{document}


which gives me the expected result:

When I then uncomment the line to fill the region between the dashed lines

      \addplot+[mark=none, update limits=false, fill=black!10] fill between [of=errmin and errmax];


I get strange error markers (actually the bars are okay, but the markers are shifted in a strange way)

What am I doing wrong here? I am unable to figure out what is the problem here.. Is it expected behaviour?

• There seems to be an issue. It is not really the fill between that distorts the error bars, but the fact that, if you use fill between in an axis, the layers get set differently. That is to say that you do not have to add the plot to see the effect, but adding set layers=standard to the axis already causes the issue. Needless to say that it shouldn't be that way. – Schrödinger's cat Oct 3 '19 at 15:28
• thanks.. I opened an issue github.com/pgf-tikz/pgf/issues/752 if you want to contribute with your insight? – jafasi Oct 3 '19 at 18:14
• I agree it is not a solution to the problem but a workaround. It does the job for me at the moment but for pgf it would be good if it was fixed at some point. I think github is the better place for this. – jafasi Oct 3 '19 at 18:36

The mess is caused by trim axis right which (in this case) isn't needed/doesn't change the output if present or not.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=1.16,
% (added this style to shorten later calls)
vertical lines style/.style={
mark=none,
update limits=false,
color=black,
},
}
X       Xminus  Xplus
1.13    0.1     0.2
1.14    0.02    0.7
1.145   0.23    0.23
}\data
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval
\begin{document}
\begin{tikzpicture}[
%    trim axis left,
%    trim axis right    % <-- this causes the mess
]
\begin{axis}[
y=-\baselineskip,
scale only axis,
width=6.5cm,
enlarge y limits={abs=0.5},
axis y line*= middle,
y axis line style=dashed,
ytick=\empty,
axis x line*=bottom,
axis on top,    % <-- (added, so the x-axis is in front of the fill between)
]
coordinates {(1.1, -1) (1.1, \numberofrows-0.5)};
coordinates {(1.05, -1) (1.05, \numberofrows-0.5)};
coordinates {(1.15, -1) (1.15, \numberofrows-0.5)};
fill between [of=errmin and errmax];
only marks,
mark=*,
error bars/.cd,
x dir=both,
x explicit,
] table [
x=X,
y expr=\coordindex,
x error minus=Xminus,
x error plus=Xplus,
] {\data};
\end{axis}
\end{tikzpicture}
\end{document}


• thanks, this is helpful, also for the "axis on top" which solves a problem I haven't really noticed... – jafasi Oct 5 '19 at 18:55
• @Schrödinger'scat, no, the trim axis ... stuff needs to be provided to the tikzpicture options! See at the description of the options in section 4.20.1 on page 380 of the PGFPlots manual (v1.16). – Stefan Pinnow Oct 6 '19 at 2:44

Instead of fillbetween is used simple fill of rectangle which borders are determined on the same way as at fillbetween:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
X Xminus Xplus
1.13 0.1 0.2
1.14 0.02 0.7
1.145 0.23 0.23
}\data
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{document}
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width             = 6.5cm,
enlarge y limits  = {abs=0.5},
axis y line*      = middle,
y axis line style = dashed,
ytick             = \empty,
axis x line*      = bottom,
]
\fill[gray!30] (1.1,-1) coordinate (a) rectangle (1.15,\numberofrows-0.5) coordinate (b);
\draw[densely dashed] (a) -- (a |- b);
\draw[densely dashed] (b) -- (b |- a);
\addplot+[only marks, blue, thick, mark=*, solid, error bars/.cd,x dir=both, x explicit]
table [x=X,y expr=\coordindex,x error minus=Xminus, x error plus=Xplus]{\data};
\end{axis}
\end{tikzpicture}
\end{document}

• thanks, that is a viable workaround, if one move the addplot-line down below the draw lines such that the rectangle doesn't hide the error bars (as in your image). Do you know what has gone wrong in my case? Shouldn't it work as well? – jafasi Oct 3 '19 at 13:52
• @jafasi, now code is corrected. – Zarko Oct 3 '19 at 13:55