0

My data source contains lines with unplottable data. More precisely, the comma separated file reads like this for a few lines at the beginning:

0.0 , , , , , , , , ,
0.05, , , , , , , , ,
0.1 , , , , , , , , , 
0.15, , , , , , , , , 
0.2 , , , , , , , , , 

If I draw a diagram from this data, the data points (cirles) corresponding to these values, 0.0 ≤ x ≤ 0.2, are missing as they should.

However, the error bars are drawn in the wrong places. They should be missing for the same x values, and present for 0.25≤x≤1 (just like the points). Instead, they are instead drawn for 0.0≤x≤0.75. The error bars are shifted to the left by the number of empty rows in the data.

enter image description here

How can I get the error bars to still correspond to their data, even in the face of unplottable data in the input file?

Sidenote: Also the bars’ y position is off, but that can be fixed by specifying a y axis range of ymin=0, ymax=1 explicitly. I’ve left it out to make the example more minimal. Also the image looks cropped here, but that is not the problem, either, as the graph is trimmed to contain only the area in which plottable data occurs, namely 0.25 ≤ x ≤ 1

Expected Output

Here is an image of the output I seek to achieve. The circles from the picture above are represented by small horizontal lines in the image below.

The image also contains a bar graph and a left-hand ordinate, which are not of concern in this question (no problems with that – I left them out for brevity).Note that there is nothing plotted for x=0.2, neither a bar, nor a value (circle / horizontal line) nor an error bar. Expected output

MWE

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}
        \newcommand{\file}{data.csv}

        \begin{axis}[
            table/col sep=comma,
            width=10cm,
            axis y line*=right,
        ]
            \addplot [
                only marks, red!100!black,
                shift={(-4pt,0)},
                error bars/.cd, y dir=both, y explicit]
                table[
                    x={x},
                    y={5 y},
                    y error minus = {5 low err},
                    y error plus = {5 high err}
            ]{\file};
            \addplot [only marks, green!70!black,
                shift={(0,0)},
                error bars/.cd, y dir=both, y explicit
            ]
                table[
                    x={x},
                    y={7 y},
                    y error minus={7 low err},
                    y error plus ={7 high err}
            ]{\file};
            \addplot [only marks, blue!100!white,
                shift={(4pt,0)},
                error bars/.cd,y dir=both, y explicit,
            ]
                table[
                    x={x},
                    y={10 y},
                    y error minus = {10 low err},
                    y error plus ={10 high err},
            ]{\file};
        \end{axis}
    \end{tikzpicture}

\end{document}

Data File

The data is provided as comma separated values. The first column contains the x axis the rest is to be plotted against. Columns 4, 7, and 10 (‘5 y’, ‘7 y’, and ‘10 y’, respectively) contain the ‘actual’ data that is shown as circles in the first image and as short horizontal lines in the second.

Columns 2, 5, and 8 (‘5 low err’, ‘7 low err’, and ‘10 low err’, respectively) contain the lower bounds for the error bars. Columns 3, 6, and 9 (‘5 high err’, ‘7 high err’, and ‘10 high err’, respectively) contain the upper bounds for the error bars

The error bounds are given relative to the plotted ‘actual’ value. For instance, from line 7 we read: At x position 0.25 do not plot the series ‘5 y’ or its corresponding error bar; do plot the value 0.222 for series ‘7 y’, and an error bar from 0.222 to 0.222; do plot the value 0.146 (as circle) for series ‘10 y’, and an error bar from 0.146−0.014 (=0.132) to 0.146+0.0756 (=0.2216) relative to the plotted value.

data.csv:

x,  5 low err,  5 high err,  5 y,  7 low err,  7 high err,  7 y,  10 low err,  10 high err,  10 y
0,,,,,,,,,
0.05,,,,,,,,,
0.1,,,,,,,,,
0.15,,,,,,,,,
0.2,,,,,,,,,
0.25,,,,0,0,0.222,0.014,0.0756,0.146
0.3,,,,0.0012,0,0.223,0.031,0.0604,0.163
0.35,,,,0,0,0.222,0.0324,0.0572,0.165
0.4,,,,0.0012,0,0.223,0.0351,0.0562,0.167
0.45,0.0078,0.0001,0.312,0.0184,0.0924,0.219,0.0856,0.1201,0.192
0.5,0.2493,0.5171,0.458,0.2493,0.5171,0.458,0.2493,0.5171,0.458
0.55,0.2493,0.5171,0.458,0.2493,0.5171,0.458,0.2493,0.5171,0.458
0.6,0,0,0.975,0,0,0.975,0,0,0.975
0.65,0,0,0.975,0,0,0.975,0,0,0.975
0.7,0,0,0.975,0,0,0.975,0,0,0.975
0.75,0,0,0.975,0,0,0.975,0,0,0.975
0.8,0,0,0.975,0,0,0.975,0,0,0.975
0.85,0,0,0.975,0,0,0.975,0,0,0.975
0.9,0,0,0.975,0,0,0.975,0,0,0.975
0.95,0,0,0.975,0,0,0.975,0,0,0.975
1,0,0,0.975,0,0,0.975,0,0,0.975
2
  • 2
    You may now get mad at me, but I have real trouble making sense of your question. Which values should be used if you do not provide any. Please consider making your question clearer by really explaining which behavior you want to achieve, and please make an effort in specifying which values are to be used.
    – user194703
    Nov 19, 2019 at 3:32
  • 1
    @Schrödinger'scat Not mad at all. Thank you for pointing out that the question is not phrased as clearly as I thought. I’ve updated it.
    – bleistift2
    Nov 19, 2019 at 6:45

2 Answers 2

1
+50

I am not 100% sure if I have understood your question right, so here a first a guess if this is what you mean. If yes, I'll be happy to add comments to explain how the solution works.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=25cm,
        height=10cm,
        xmin=0.25,
        enlarge x limits={abs=0.025},
        axis y line*=right,
        ybar,
        table/col sep=comma,
        table/x={x},
        error bars/y dir=both,
        error bars/y explicit,
        cycle list={
            {red!100!black,fill=.!30},
            {green!70!black,fill=.!30},
            {blue!100!white,fill=.!30},
        },
    ]
        \addplot table [
            y={5 y},
            y error minus={5 low err},
            y error plus={5 high err}
        ] {data.csv};

        \addplot table [
            y={7 y},
            y error minus={7 low err},
            y error plus={7 high err}
        ]{data.csv};

        \addplot table [
            y={10 y},
            y error minus={10 low err},
            y error plus={10 high err},
        ]{data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • Your code was very helpful in cleaning up the axis options. Using your code example, I was able to find the culprit by increasingly assimilating the MWE to my original problem, so I’ll award you the bounty. But since your answer actually circumvented the problem, I’ll mark my own answer as accepted.
    – bleistift2
    Nov 21, 2019 at 9:02
0

It turns out that the shift option to the addplot commands were malformed. The y coordinate is lacking a unit. Strangely, this did not produce an error or warning in the output. Changing shift={(4pt,0)} (and the like) to shift={(4pt,0pt)} produced the expected output: The error bars are drawn in the same x positions as the data points.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .