tikzpicture 带误差线的条形图(最小值,最大值)

tikzpicture 带误差线的条形图(最小值,最大值)

我想将最小值和最大值添加到下图中。到目前为止,我在网上找不到解决方案。

如果可能的话,我想以特定的格式写入我的数据。

谢谢您的帮助,我不太熟悉在 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

在此处输入图片描述

\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}

答案1

欢迎!这添加了误差线。我不确定您想如何提供误差,所以暂时我只是从您的表格中添加了六列,以指示误差线的尺寸(+ 和 -)。

\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}

在此处输入图片描述

答案2

多谢薛定谔的猫

我无法及时向您提供错误格式。我稍微修改了您的代码以适应我想要的错误格式。这种错误格式更容易从我的数据集生成。我在您的代码中添加了绘制行 N 到 M 的图表,解释如下这里

再次感谢!

\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}

在此处输入图片描述

相关内容