3

I would like to add the minimum and maximum values to the plot shown below. Sofar I could not find the solution online.

If possible I would like to write my data in a specific format.

Thanks for any help, I am not really familiar with plotting in Latex.

Desired format for "Data1" as an example:

avg  min  max
2.0  1.5  3.0
2.0  1.5  3.0
2.0  1.5  3.0
2.0  1.5  3.0

enter image description here

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{external}

\begin{document}

\pgfplotstableread{
0 2 3 4  
1 2 3 4
2 2 3 4  
3 2 3 4     
}\dataset

\begin{tikzpicture}

\begin{axis}[ybar,
        width=.9\textwidth,
        ymin=0,
        ymax=5,        
        ylabel={Y-Label},
        xtick=data,
        xticklabels = {
            Category 1,
            Category 2,
            Category 3,
            Category 4
        },
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        ]
\addplot[draw=black,fill=black!20] table[x index=0,y index=1] \dataset; %Data1
\addplot[draw=black,fill=black!40] table[x index=0,y index=2] \dataset; %Data2
\addplot[draw=black,fill=black!60] table[x index=0,y index=3] \dataset; %Data3

\legend{Data1, Data2, Data3}
\end{axis}

\end{tikzpicture}

\end{document}
Stefan Pinnow
  • 29,535

2 Answers2

2

Welcome! This adds the error bars. I was not sure how you want to provide the errors, so for the time being I just added six columns from your table which indicate the dimensions (+ and -) of the error bars.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread{
0 2 3 4 1 0.5 0.5 1 0.5 1
1 2 3 4 1 0.5 0.5 1 0.5 1
2 2 3 4 1 0.5 0.5 1 0.5 1
3 2 3 4 1 0.5 0.5 1 0.5 1    
}\dataset

\begin{tikzpicture}
\begin{axis}[ybar,
        width=.9\textwidth,
        ymin=0,
        ymax=5,        
        ylabel={Y-Label},
        xtick=data,
        xticklabels = {
            Category 1,
            Category 2,
            Category 3,
            Category 4
        },
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        legend style={at={(0.9,0.99)},anchor=north east}
        ]
\addplot[draw=black,fill=black!20,error bars/.cd,y dir=both,y explicit] 
    table[x index=0,y index=1,y error plus index=4,y error minus index=5] \dataset; %Data1
\addplot[draw=black,fill=black!40,error bars/.cd,y dir=both,y explicit] 
    table[x index=0,y index=2,y error plus index=6,y error minus index=7] \dataset; %Data2
\addplot[draw=black,fill=black!60,error bars/.cd,y dir=both,y explicit] 
    table[x index=0,y index=3,y error plus index=8,y error minus index=9] \dataset; %Data3
\legend{Data1, Data2, Data3}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1

Thanks a lot Schrödinger's cat !

I was not able to supply the error format in time to you. I have slightly altered your code to work with my desired error format. This error format is a bit easier to generate from my dataset. I added plotting rows N to M to your code, which is explained here.

Thanks again!

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

% Data1 ranges from row [0,2] 
% Data2 ranges from row [3,5]
% Data3 ranges from row [6,8]
\pgfplotstableread{
x       y    y+    y-
200     2    1.0   0.5
400     2    1.0   0.5
600     2    1.0   0.5  
200     3    1.0   0.5
400     3    1.0   0.5
600     3    1.0   0.5  
200     4    1.0   0.5
400     4    1.0   0.5
600     4    1.0   0.5  
}{\mytable}

\begin{tikzpicture}
    \begin{axis}[ybar,
            width=.9\textwidth,
            ymin=0,
            ymax=6,        
            ylabel={Y-Label},
            xtick=data,
            xticklabels = {
                Category 1,
                Category 2,
                Category 3
            },
            major x tick style = {opacity=0},
            minor x tick num = 1,
            minor tick length=2ex,
            legend style={at={(0.9,0.99)},anchor=north east}
            ]

            \addplot [ybar, fill=black!20] 
            plot [error bars/.cd, y dir=both, y explicit]
            table[x index=0, y index=1, select coords between index={0}{2}, y error plus=y+, y error minus=y-]{\mytable}; % Data1

            \addplot [ybar, fill=black!40] 
            plot [error bars/.cd, y dir=both, y explicit]
            table[x index=0, y index=1, select coords between index={3}{5}, y error plus=y+, y error minus=y-]{\mytable}; % Data2

            \addplot [ybar, fill=black!40] 
            plot [error bars/.cd, y dir=both, y explicit]
            table[x index=0, y index=1, select coords between index={6}{8},y error plus=y+, y error minus=y-]{\mytable}; % Data3

    \legend{Data1, Data2, Data3}
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here