嵌套或组栏组

嵌套或组栏组

我正在尝试构建分组条形图并有以下问题:

我必须有多个条形图(4)Turquoise, Purple, Red, Blue, Green。此外,我可以拆分属于每个的 4 个条形图吗Turquoise, Purple, Red, Blue, Green?下面您可以看到代码,灵感来自邮政。此外,我还发布了原始输出以及预期输出。

以下是代码:

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.4}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotstableread{
Criterion   One         Two         Three
Turquoise   3620        15749       42754
%Yellow      2739        15109       42240
Purple      3710        16146       40533
Red         714         9306        28919
Blue        2997        6840        11614
%Orange      1589        4992        11786
Green       2122        11154        28747
}\datatable

\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}

\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}

\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}

\begin{document}



\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
    [
        group style=
            {
            columns=3,
            xlabels at=edge bottom,
            ylabels at=edge left,
            horizontal sep=0.05\textwidth, group name=plots
            },
        ybar=0pt,
        /pgf/bar shift=0pt,scale only axis,
        width=0.74\textwidth/3,
        yticklabel style={text width=0.035\textwidth, align=right, inner xsep=0pt, xshift=-0.005\textwidth},
        ylabel=Performance, ylabel style={text height=0.02\textwidth,inner ysep=0pt},
        enlarge x limits=0.15,
        ymin=0,
        xtick=\empty,
        tickpos=left,
        scaled y ticks=base 10:-3,
        cycle list name=colorbrewer-RYB,
        legend columns=-1,
        legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
        legend image code/.code={%
             \draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
        }
    ]

\nextgroupplot[xlabel=One, legend to name=grouplegend,]
\pgfplotsinvokeforeach  {0,...,4}{
    \addplot table [ x expr=\coordindex, select row=#1, y=One] {\datatable};
    \addlegendentry {\pgfplotstablegetelem{#1}{Criterion}\of\datatable \pgfplotsretval}
}

\nextgroupplot[xlabel=Two]
\pgfplotsinvokeforeach  {0,...,4}{
    \addplot table [x expr=\coordindex, select row=#1, y=Two] {\datatable};
}

\nextgroupplot[xlabel=Three]
\pgfplotsinvokeforeach  {0,...,4}{
    \addplot table [x expr=\coordindex, select row=#1, y=Three] {\datatable};
}

\end{groupplot}
\node at (plots c2r1.south) [inner sep=0pt,anchor=north, yshift=-5ex] {\ref{grouplegend}};
\end{tikzpicture}
\end{figure}


\end{document}

原始输出

在此处输入图片描述

预期输出

在此处输入图片描述

答案1

如果我理解正确的话,将表格转置为\pgfplotstabletranspose,并在单独的轴上绘制新表格的列。有很多小的变化,请参阅代码中的注释。

代码输出

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17} % <-- increased from 1.4
\usetikzlibrary{pgfplots.groupplots}

\pgfplotstableread{
Criterion   One         Two         Three
Turquoise   3620        15749       42754
%Yellow      2739        15109       42240
Purple      3710        16146       40533
Red         714         9306        28919
Blue        2997        6840        11614
%Orange      1589        4992        11786
Green       2122        11154        28747
}\datatable

\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}

\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}

\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}

\begin{document}


\begin{figure}
% transpose table
\pgfplotstabletranspose[colnames from=Criterion]{\datatableT}{\datatable}
% typesetting the table can sometimes be useful for debugging
%\pgfplotstabletypeset[string type]{\datatableT}
\begin{tikzpicture}

\begin{groupplot}
    [
        group style=
            {
            columns=5,
            xlabels at=edge bottom,
            y descriptions at=edge left, % only ticklabels on left side
            horizontal sep=0.01\textwidth, % less space between
            group name=plots
            },
        ybar=0pt,
        /pgf/bar shift=0pt,
        /pgf/bar width=0.8, % <-- added sets bar width in axis units, requires compat=1.7
        scale only axis,
        width=0.87\textwidth/5, % <-- reduce width to avoid overfull hbox
        height=\textwidth/5, % <-- increase height a bit 
        ylabel=Performance,
        ylabel style={
           text height=0.02\textwidth,
           inner ysep=0pt
           },
        enlarge x limits={abs=0.5}, % <-- modified, half an axis unit of space on each side
        ymin=0,ymax=45000, % <-- common ymax for all axes
        xtick={0,...,2},  % <-- explicit tick locations
        xticklabels from table={\datatableT}{colnames}, % <-- read ticklabels from the table
        xticklabel style={rotate=45}, % rotate to make room
        tickpos=left,
        scaled y ticks=base 10:-3,
        cycle list name=colorbrewer-RYB,
        legend columns=-1,
        legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
        legend image code/.code={%
             \draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
        },
        y tick scale label style={at={(yticklabel* cs:1.03, 2em)}}, % <-- move scale label to the left a bit
        title style={anchor=base} % <-- change anchor to improve alignment
    ]


\nextgroupplot[title=Turquoise]
\addplot table[x expr=\coordindex, y=Turquoise] {\datatableT};

\nextgroupplot[title=Purple]
% go one step ahead in cycle list to get right colour
\pgfplotsset{cycle list shift=1}
\addplot table[x expr=\coordindex, y=Purple] {\datatableT};

\nextgroupplot[title=Red]
\pgfplotsset{cycle list shift=2}
\addplot table[x expr=\coordindex, y=Red] {\datatableT};

\nextgroupplot[title=Blue]
\pgfplotsset{cycle list shift=3}
\addplot table[x expr=\coordindex, y=Blue] {\datatableT};

\nextgroupplot[title=Green]
\pgfplotsset{cycle list shift=4}
\addplot table[x expr=\coordindex, y=Green] {\datatableT};
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容