2

I have the following plot:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfplotstableread{
  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:

okay plot but without fill

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)

graph with strange errors

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

  • 1
    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
3

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,
        },
    }
    \pgfplotstableread{
        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)
    ]
        \addplot [vertical lines style]
            coordinates {(1.1, -1) (1.1, \numberofrows-0.5)};
        \addplot [vertical lines style,dashed,name path=errmin]
            coordinates {(1.05, -1) (1.05, \numberofrows-0.5)};
        \addplot [vertical lines style,dashed,name path=errmax]
            coordinates {(1.15, -1) (1.15, \numberofrows-0.5)};
        \addplot [fill=black!10]
            fill between [of=errmin and errmax];
        \addplot+ [
            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}

image showing the result of above code

|improve this answer|||||
2

enter image description here

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}
\pgfplotstableread{
  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}
|improve this answer|||||
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.