[Tex/LaTex] plot an average-line plot like this with PGFPlots

pgfplotstikz-pgf

I would like to create a plot like this (maybe a bit better looking – this is my attempt):

enter image description here

Here is how the data for the plot looks:

x,min,max,avg
0,800,3600,2628.571429
1.5,2000,3600,2942.857143
2.0,3000,3600,3300
3.0,3200,4000,3885.714286
4.5,3200,5000,4414.285714
  1. Can I create a plot like this with pgfplots (i've looked for a plot type like this but could not find it anywhere)?
  2. Is there any way to get rid of the ugly "scaling" that I do (i.e. divide by 1000)?
  3. Is there a better way refer to DTLcurrentindex without writing it out in full every time?
  4. Is there a better way to read the data from the file?
  5. Any other glaring mistakes?

My code follows:

\documentclass{article}

    \usepackage{tikz}

    \usetikzlibrary{arrows.meta}
    \usepackage{datatool}

    \usetikzlibrary{arrows,backgrounds,snakes}
    \begin{document}

    \begin{tikzpicture}[>=Stealth]
      \DTLloaddb{data}{moose.csv}
      \DTLforeach*{data}{\x=x,\min=min, \m=max, \avg=avg}{
        % Draw the top whisker
        \draw (\DTLcurrentindex - 0.2, \m/1000) -- (\DTLcurrentindex + 0.2, \m/1000);
        % Draw the bottom whisker
        \draw (\DTLcurrentindex - 0.2, \min/1000) -- (\DTLcurrentindex + 0.2, \min/1000);
        % Draw the vertical line
        \draw (\DTLcurrentindex, \min/1000) -- (\DTLcurrentindex, \m/1000);
        % Draw the middle line
        \draw (\DTLcurrentindex - 0.1, \avg/1000) -- (\DTLcurrentindex + 0.1, \avg/1000);

        % Draw the x-axis and y-axis
        \draw[->] (0,0) -- (6,0);
        \draw[->] (0,0) -- (0,6);
        \foreach \tick in {1,2,3,4,5}{
          \draw (-0.05, \tick) -- (0.05, \tick);
        }

        % Draw the x tick and label
        \draw (\DTLcurrentindex, 0.05) -- (\DTLcurrentindex, -0.05) node[below]{\x};
      }
    \end{tikzpicture}

    \end{document} 

Best Answer

Do you mean something like this ...

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
    % store data in TXT file
    \begin{filecontents}{data.txt}
        x,min,max,avg
        0,800,3600,2628.571429
        1.5,2000,3600,2942.857143
        2.0,3000,3600,3300
        3.0,3200,4000,3885.714286
        4.5,3200,5000,4414.285714
    \end{filecontents}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot [
                % set mark style
                mark=-,
                % draw only the markers (and no connecting line)
                only marks,
                % define that error bars are there in y both directions
                % and that the values are given explicitly
                error bars/.cd,
                    y dir=both,
                    y explicit,
            ]
                table [
                    x=x,
                    y=avg,
                    % because in the table not the error value but
                    % the min and max values are given
                    % --> calculate the corresponding explicit error values
                    y error minus expr=\thisrow{avg}-\thisrow{min},
                    y error plus expr=\thisrow{max}-\thisrow{avg},
                    col sep=comma,
                ] {data.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code