从文件中选择某些行的 pgfplots 分组条形图

从文件中选择某些行的 pgfplots 分组条形图

我想根据以下数据创建一个条形图,并使用列difficulty从中选择行kappa

我希望避免在一系列添加绘图语句中输入 csv 文件的内容

可以按如下方式使用元变量吗?

\begin{axis}[ybar]

 \begin{tikzpicture}
\begin{axis}[ybar,ymajorgrids, tick align=inside,
    major grid style={draw=white},axis x line*=bottom,
    axis y line*=right,symbolic x coords={
       Human-Human,Human-Tak,Human-Gold Standard, Tak-Gold Standard},]
    \addplot table[x=type, y=kappa, meta=difficulty=0]{combined-kappas.csv};
    \addplot table[x=type, y=kappa, meta=difficulty=1]{combined-kappas.csv};
    \addplot table[x=type, y=kappa, meta=difficulty=2]{combined-kappas.csv};
\end{axis}

CSV 数据:

r1,r2,type,kappa,difficulty
Alex,Tak,Human-Human,0.9878019029031471,0
Alex,Inferred,Human-Tak,0.6199032480995161,0
Alex,Intended,Human-Gold Standard,1,0
Tak,Inferred,Human-Tak,0.6081143384047948,0
Tak,Intended,Human-Gold Standard,0.9878019029031471,0
Intended,Inferred,Tak-Gold Standard,0.6199032480995161,0
Alex,Tak,Human-Human, 0.791208791208791,1
Alex,Inferred,Human-Tak,0.306901088386556,1
Alex,Intended,Human-Gold Standard,0.844389844389844,1
Tak,Inferred,Human-Tak,0.245305729417429,1
Tak,Intended,Human-Gold Standard,0.757327238504975,1
Intended,Inferred,Tak-Gold Standard,0.337421837421837,1
Alex,Tak,Human-Human,0.547137761168677,2
Alex,Inferred,Human-Tak,0.137205030380104,2
Alex,Intended,Human-Gold Standard,0.262046204620462,2
Tak,Inferred,Human-Tak,0.137288621381485,2
Tak,Intended,Human-Gold Standard,0.280682000965096,2
Intended,Inferred,Tak-Gold Standard,0.097688926697859,2

预期输出: 在此处输入图片描述

答案1

回答你在问题下方的评论
我认为链接的答案我的评论你在搜索什么。但是剩下的问题是,有时一个条形图有多个条目,这会导致绘制多个条形图。在您的图片中,条形图带有某种误差线。

误差线也可以提供给条形图,但当然为此您需要相应地修改 CSV 文件。

(请注意,这个答案只是为了证明这在原则上就是你正在寻找的,因此生成的图像看起来不太好看。)

% used PGFPlots v1.16
\begin{filecontents*}{combined-kappas.csv}
r1,r2,type,kappa,difficulty
Alex,Tak,Human-Human,0.9878019029031471,0
Alex,Inferred,Human-Tak,0.6199032480995161,0
Alex,Intended,Human-Gold Standard,1,0
Tak,Inferred,Human-Tak,0.6081143384047948,0
Tak,Intended,Human-Gold Standard,0.9878019029031471,0
Intended,Inferred,Tak-Gold Standard,0.6199032480995161,0
Alex,Tak,Human-Human, 0.791208791208791,1
Alex,Inferred,Human-Tak,0.306901088386556,1
Alex,Intended,Human-Gold Standard,0.844389844389844,1
Tak,Inferred,Human-Tak,0.245305729417429,1
Tak,Intended,Human-Gold Standard,0.757327238504975,1
Intended,Inferred,Tak-Gold Standard,0.337421837421837,1
Alex,Tak,Human-Human,0.547137761168677,2
Alex,Inferred,Human-Tak,0.137205030380104,2
Alex,Intended,Human-Gold Standard,0.262046204620462,2
Tak,Inferred,Human-Tak,0.137288621381485,2
Tak,Intended,Human-Gold Standard,0.280682000965096,2
Intended,Inferred,Tak-Gold Standard,0.097688926697859,2
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % borrowed styles from Jake's answer at
    % <http://tex.stackexchange.com/a/301991/95441>
    \pgfplotsset{
%        % filter symbolic values
%        discard if not symbolic/.style 2 args={
%            % suppress LOG messages about the filtered points
%            filter discard warning=false,
%            x filter/.append code={
%                \edef\tempa{\thisrow{#1}}
%                \edef\tempb{#2}
%                \ifx\tempa\tempb
%                \else
%                    \def\pgfmathresult{NaN}
%                \fi
%            },
%        },
        % filter numerical values
        % (not needed here, but just for completeness)
        discard if not/.style 2 args={
            % suppress LOG messages about the filtered points
            filter discard warning=false,
            x filter/.append code={
                \ifdim\thisrow{#1} pt=#2pt
                \else
                    \def\pgfmathresult{NaN}
                \fi
            },
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            ybar,
            symbolic x coords={
                Human-Human,
                Human-Tak,
                Human-Gold Standard,
                Tak-Gold Standard
            },
            enlarge x limits=0.2,
        ]
            \foreach \i in {0,1,2} {
                \addplot+ [
                    discard if not={difficulty}{\i},
                ] table [
                    col sep=comma,
                    x=type,
                    y=kappa,
                ] {combined-kappas.csv};
                    \addlegendentryexpanded{\i}
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容