如何将多张图表分组为一张图表?

如何将多张图表分组为一张图表?

我有几张图表想要通过条形图来表示。最初,我考虑将图表分组在一起(如图 A 所示),但由于我将有几张不同的图表,为了节省空间,我想将它们合并成一张大图表;类似于这里(也显示在图 B 中)。基本上,它应该是一张图表(xy 轴),结合了当前图表中的所有数据;但每个部分应该分开(可能通过垂直轴线)。我不知道该怎么做,可能吗?怎么做?

更多信息:我试图绘制的图表基于一个不太大的数据集。值 20、40、50、60、80 是百分点。图表中的当前条形图在 30%、70% 的值处有间隙,我不希望存在这个间隙。条形图可以像图 B 中显示的条形图一样接近。由于我有 10、20、30、40、50F 的数据(如表格名称所示),如果每个条形图子组在 xticks 下都有一个标签并按此顺序排列,那就太好了。除此之外,任何重新排列/添加都是可以的。 更新:是的,允许重新组织数据文件(无法在评论中发布)。

Tex 文件

图片A: 在此处输入图片描述

图片B: 在此处输入图片描述

我的分组图代码:

\begin{document}

\begin{figure}

    \centering
    \begin{tikzpicture}
        \begin{groupplot}[
            legend columns=-1,
            legend entries={{\color{red}{\tiny Algirthm A}}, {\color{blue}{\tiny Algorithm B}}},
            legend to name=CombinedLegendBar,
            footnotesize,
            area legend,
            ymin=0,
            ymax=35,
            ylabel= {},
            xlabel= {},
            xtick=data,
            xtick pos=left,
            ytick pos=left,
            ymajorgrids,
            group style={
                rows=3,
                columns=2,
                %xlabels at=edge bottom,
                %ylabels at=edge left,
                %xticklabels at=edge bottom,
                horizontal sep=50pt,
                vertical sep=30pt
            }
        ]

             \nextgroupplot[title={\scriptsize 10 Flows} ]
            \addplot[ybar,draw=red,pattern color=red, pattern=dots] table[x={n}, y={p}] {10FA.dat};
            \addplot[ybar,draw=blue,pattern color=blue, pattern=north east lines] table[x={n}, y={p}] {10FB.dat};

        \nextgroupplot[title={\scriptsize 20 Flows} ]
            \addplot[ybar,draw=red,pattern color=red, pattern=dots] table[x={n}, y={p}] {20FA.dat};
            \addplot[ybar,draw=blue,pattern color=blue, pattern=north east lines] table[x={n}, y={p}] {20FB.dat};

        \nextgroupplot[title={\scriptsize 30 Flows} ]
            \addplot[ybar,draw=red,pattern color=red, pattern=dots] table[x={n}, y={p}] {30FA.dat};
            \addplot[ybar,draw=blue, pattern color = blue, pattern=north east lines] table[x={n}, y={p}] {30FB.dat};

        \nextgroupplot[title={\scriptsize 40 Flows} ]
            \addplot[ybar,draw=red,pattern color=red, pattern=dots] table[x={n}, y={p}] {40FA.dat};
            \addplot[ybar, draw=blue, pattern color =blue, pattern=north east lines] table[x={n}, y={p}] {40FB.dat};

        \nextgroupplot[title={\scriptsize 50 Flows},
                        at = { ($ ( $ (group c1r2.south west) + (0,-50pt)$ )!0.5!(group c2r2.south east) $ ) } ]
            \addplot[ybar,draw=red,pattern color=red, pattern=dots] table[x={n}, y={p}] {50FA.dat};
            \addplot[ybar,draw=blue, pattern color=blue, pattern=north east lines] table[x={n}, y={p}] {50FB.dat};

        \end{groupplot}
        \node[xshift=-1.2cm] at ($(group c1r1.west)!0.5!(group c1r2.west)$) {\rotatebox{90}{{\Large Y Axis Label}}};
        \node[yshift=-1cm]  at (group c1r3.south){{\Large X Axis Label}};
        \node[yshift=-2cm] at (group c1r3.south){\ref{CombinedLegendBar}};
    \end{tikzpicture}

\end{figure}

\end{document}

答案1

希望您的意思是以下内容。

有关解决方案如何运作的更多详细信息,请查看代码中的注释。

% restructured data file:
% - 'n' values are sorted ascending
% - added column 'z' to know which data belong to which measurement
% - added a dummy line at the end of each data set to produce an empty entry
%   for the separation of the blocks
% - put all data in one file
\begin{filecontents}{data.txt}
z   n   pFA pFB
10  2   6   1
10  4   7   1
10  5   8   4
10  6   9   7
10  8   10  9
20  {}  0   0
20  2   9   4
20  4   12  6
20  5   14  3
20  6   15  8
20  8   18  5
30  {}  0   0
30  2   11  5
30  4   17  5
30  5   19  5
30  6   21  6
30  8   24  8
30  {}  0   0
40  2   13  3
40  4   19  5
40  5   21  18
40  6   24  8
40  8   28  10
40  {}  0   0
50  2   15  4
50  4   21  12
50  5   24  4
50  6   27  9
50  8   30  10
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{patterns}
    \pgfplotsset{compat=1.3}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            footnotesize,
            % set the `width' of the plot to the maximum length ...
            width=\textwidth,
            % ... and use half this length for the `height'
            height=0.5\textwidth,
            ymin=0,
            ymax=35,
            % use `data' for the positioning of the `xticks' ...
            xtick=data,
            % ... and use table data for labeling the `xticks'
            xticklabels from table={data.txt}{n},
            % add extra ticks "at the empty entries to add the vertical lines
            extra x ticks={5,11,17,23},
            % this ticks shouldn't be labeled ...
            extra x tick labels={},
            % ... but grid lines should be drawn without the tick lines
            extra x tick style={
                grid=major,
                major tick length=0pt,
            },
            xlabel={X Axis Label},
            ylabel={Y Axis Label},
            % because of the category labels, shift the `xlabel' a bit down
            xlabel style={
                yshift=-4ex,
            },
            legend pos=north west,
            legend entries={
                {\color{red}{Algorithm A}},
                {\color{blue}{Algorithm B}},
            },
            area legend,
            % adjust `bar width' so it fits your needs ...
            bar width=8pt,
            % ... and with that you also have to adjust the x limits
            enlarge x limits={abs=1},
            % set `clip mode' to `individual' so the category labels aren't clipped away
            clip mode=individual,
        ]

        % plot the "red" ybars
            \addplot [
                ybar,
                draw=red,
                pattern color=red,
                pattern=dots,
            ] table [
                % use just the `coordindex' as x coordinate,
                % the correct labeling is done with `xticklabels from table'
                x expr=\coordindex,
                y=pFA,
            ] {data.txt};

        % plot the "blue" ybars
            \addplot [
                ybar,
                draw=blue,
                pattern color=blue,
                pattern=north east lines,
            ] table [
                x expr=\coordindex,
                y=pFB,
            ] {data.txt};

        % add the category labels
            \begin{scope}[
                % because the reference point will be the lower axis line the
                % labels have to be moved a bit more down to don't overlap with
                % the `xticklabels'
                every label/.append style={
                    label distance=2ex,
                },
            ]
                \node [label=below:10 Flows]
                    at (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) {};
                \node [label=below:20 Flows]
                    at (axis cs:8,\pgfkeysvalueof{/pgfplots/ymin}) {};
                \node [label=below:30 Flows]
                    at (axis cs:14,\pgfkeysvalueof{/pgfplots/ymin}) {};
                \node [label=below:40 Flows]
                    at (axis cs:20,\pgfkeysvalueof{/pgfplots/ymin}) {};
                \node [label=below:50 Flows]
                    at (axis cs:26,\pgfkeysvalueof{/pgfplots/ymin}) {};
            \end{scope}

        \end{axis}
    \end{tikzpicture}
\end{document}

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

相关内容