如何在两个条形图之间留出空间

如何在两个条形图之间留出空间

我想绘制两个条形图,彼此之间有更多空间(现在 y 轴标签与第一个图重叠)。我还想为每个图添加 x 轴标签和标题。有人能帮我解决这个问题吗?代码:

 \documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=1.16}


\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[group style={group size=2 by 1},width=12cm,height=8cm]
        \nextgroupplot[
            %legend style={at={(mygroup.south)},anchor=north,legend columns=-1},
            symbolic x coords={Precision,Recall,F1-Score,Accuracy},
            major tick length=0cm,
            xtick=data,
            ymin=50.0,
            ylabel = {y},
            xlabel = {x},
            enlarge x limits=0.2,
            enlarge y limits={upper,value=0.2},
            nodes near coords,
            ybar,
            every node near coord/.append style={rotate=90, anchor=west},
            bar width = 8pt,
        ]

        \addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
        \addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
        \addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
        \addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
        \addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
        \addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};
        %\legend{KNN,Neural Networks,Decision Tree,Random Forest,SVM,Naive Bayes}

        \nextgroupplot[
            legend style={at={(-\pgfkeysvalueof{/pgfplots/group/horizontal sep}/2,-0.1)},
                    anchor=north,legend columns=-1},
            symbolic x coords={Precision,Recall,F1-Score,Accuracy},
            major tick length=0cm,
            xtick=data,
            ymin=50.0,
            ylabel = {y},
            enlarge x limits=0.2,
            enlarge y limits={upper,value=0.2},
            nodes near coords,
            ybar,
            every node near coord/.append style={rotate=90, anchor=west},
            bar width = 8pt,
        ]

        \addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
        \addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
        \addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
        \addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
        \addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
        \addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};
        



        \legend{KNN,Neural Networks,Decision Tree,Random Forest,SVM,Naive Bayes}
    \end{groupplot}
    
    
    
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我建议看看pgfplots 手册(5.8 分组图)。斜体段落直接取自手册。

1. 空间

  • group/horizontal sep=<dimension>
  • group/vertical sep=<dimension>

我们设置分别表示水平和垂直方向上绘图之间的间距。如果您希望将它们粘合在一起,则应将它们的长度都设置为 0pt。

2.X 标签

  • group/x descriptions at=all|edge top|edge bottom
  • group/y descriptions at=all|edge left|edge right

我们可以设置 x 和 y 标签的位置。这些只是同时使用 xticklabels at 和 xlabels at 的简写。问题是我们将标题放在上面,将图例放在下面。所以我们只能将 x 标签放在每个 x 轴的右侧。为此,我们首先group/group name={<name>}groupplot环境中进行设置。之后我们可以参考情节

% bar chart 1
\node[xshift=.75cm, yshift=-.2cm] at (myResults c1r1.south east) {x-Label};
% bar chart 2
\node[xshift=.75cm, yshift=-.2cm] at (myResults c2r1.south east) {x-Label};

3. 标题

title={<title>}要设置标题,我们可以向每个标题添加选项\nextgroupplot

完整代码:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=1.16}


\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[group style={group name=myResults, group size=2 by 1, horizontal sep=2.5cm},width=12cm,height=8cm, ylabel = {y}]
        \nextgroupplot[
            title={Title 1},
            %legend style={at={(mygroup.south)},anchor=north,legend columns=-1},
            symbolic x coords={Precision,Recall,F1-Score,Accuracy},
            major tick length=0cm,
            xtick=data,
            ymin=50.0,
            enlarge x limits=0.2,
            enlarge y limits={upper,value=0.2},
            nodes near coords,
            ybar,
            every node near coord/.append style={rotate=90, anchor=west},
            bar width = 8pt
        ]

        \addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
        \addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
        \addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
        \addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
        \addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
        \addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};
        %\legend{KNN,Neural Networks,Decision Tree,Random Forest,SVM,Naive Bayes}

        \nextgroupplot[
            title={Title 2},
            legend style={at={(-\pgfkeysvalueof{/pgfplots/group/horizontal sep}/2,-0.1)},
                    anchor=north,legend columns=-1},
            symbolic x coords={Precision,Recall,F1-Score,Accuracy},
            major tick length=0cm,
            xtick=data,
            ymin=50.0,
            enlarge x limits=0.2,
            enlarge y limits={upper,value=0.2},
            nodes near coords,
            ybar,
            every node near coord/.append style={rotate=90, anchor=west},
            bar width = 8pt,
        ]

        \addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
        \addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
        \addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
        \addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
        \addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
        \addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};
        



        \legend{KNN,Neural Networks,Decision Tree,Random Forest,SVM,Naive Bayes}
    \end{groupplot}

    % x axis label
    \node[xshift=.75cm, yshift=-.2cm] at (myResults c1r1.south east) {x-Label};
    \node[xshift=.75cm, yshift=-.2cm] at (myResults c2r1.south east) {x-Label};
\end{tikzpicture}

\end{document}

两个并排的条形图。

答案2

还有一个解决方案,其图表代码更加简洁:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
   \begin{groupplot}[
        group style={
            group size=2 by 1,
            horizontal sep=2cm,  % <----- distance between plots
                    },
            width=12cm, height=8cm,
    major tick length=0cm,
    xtick=data,
    enlarge x limits=0.2,
    enlarge y limits={upper,value=0.2},
    ybar,
    ymin=60.0,
    symbolic x coords={Precision,Recall,F1-Score,Accuracy},
    nodes near coords,
    every node near coord/.append style={rotate=90, anchor=west},
%
    legend style={at={(-\pgfkeysvalueof{/pgfplots/group/horizontal sep}/2,-0.1)},
                  anchor=north, legend columns=-1,
                  /tikz/every even column/.append style={column sep=1em}},            
                      ]
\nextgroupplot[
    bar width = 9pt,
    title = {Title 1},
    ylabel= $y$
              ]
\addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
\addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
\addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
\addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
\addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
\addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};

\nextgroupplot[
    bar width = 9pt,
    title = {Title 2},
    ylabel= $y$
            ]
\addplot coordinates {(Precision,86.6) (Recall,86.0) (F1-Score,86.3) (Accuracy,98.4)};
\addplot coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
\addplot coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
\addplot coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
\addplot coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
\addplot coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};

\legend{KNN,Neural Networks, Decision Tree,Random Forest,SVM,Naive Bayes}
    \end{groupplot}
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:x在图表具有不同和轴的 情况下y,它们的定义应该从的选项移至groupplot的选项\nextgroupplot

相关内容