0

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} 
3
  • Why did you tag this pgfplots if you do not use pgfplots? (Honest question...) Does this help: tex.stackexchange.com/q/115210/32374
    – darthbith
    Dec 5, 2014 at 21:34
  • I hoped that pgfplots solution exists - I'd much prefer ready made plot than drawing my own. Thanks, I saw that question - but I don't need boxes - just a line showing the "average". Dec 5, 2014 at 21:56
  • Right, you said that in the question... sorry... Perhaps a modification of this one then? tex.stackexchange.com/a/131613/32374
    – darthbith
    Dec 5, 2014 at 21:59

1 Answer 1

2

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

You must log in to answer this question.

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