1

I have two bar charts side by side, and they share the same legend, I have tried to use \addlegendentry{}, but I find my self with a repeated legend!

the output of the code

Here is the code, what should I do (I tried two many solution online but nothing work):

\begin{figure*}[h!]
\begin{adjustbox}{center}
\pgfplotsset{width=0.93\textwidth,height=0.4\textheight,compat=1.16}
\begin{tikzpicture}
\begin{axis}[
   legend style={at={(0.5,-0.09)},
      anchor=north,legend columns=-1},
    symbolic x coords={Precision,Recall,F1-Score,Accuracy},
    major tick length=0cm,
    xtick=data,
    ymin=75.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)};
\addlegendentry{KNN}

\addplot
coordinates {(Precision,87.6) (Recall,86.2) (F1-Score,86.5) (Accuracy,97.5)};
\addlegendentry{Neural Networks}

\addplot
coordinates {(Precision,85.9) (Recall,86.3) (F1-Score,86.1) (Accuracy,98.6)};
\addlegendentry{Decision Tree}

\addplot
coordinates {(Precision,87.1) (Recall,86.8) (F1-Score,87.0) (Accuracy,99.2)};
\addlegendentry{Random Forest}

\addplot
coordinates {(Precision,85.5) (Recall,82.8) (F1-Score,83.7) (Accuracy,97.4)};
\addlegendentry{SVM}

\addplot
coordinates {(Precision,77.9) (Recall,77.7) (F1-Score,75.9) (Accuracy,87.8)};
\addlegendentry{Naive Bayes}

\end{axis}

\end{tikzpicture}% NO EMPTY LINE HERE!!!!
\begin{tikzpicture}

\begin{axis}[
   legend style={at={(0.5,-0.09)},
      anchor=north,legend columns=-1},
    symbolic x coords={FPR,FNR},
    major tick length=0cm,
    xtick=data,
    ymin=0.0,
    enlarge x limits=0.5,
    enlarge y limits={upper,value=0.2},
    nodes near coords,
    ybar,
    every node near coord/.append style={rotate=90, anchor=west},
    bar width = 15pt,
]

\addlegendentry{KNN}
\addplot
coordinates {(FPR,7.90) (FNR,14.0)};

\addlegendentry{Neural Networks}
\addplot
coordinates {(FPR,3.45) (FNR,13.75)};

\addlegendentry{Decision Tree}
\addplot
coordinates {(FPR,0.39) (FNR,13.63)};

\addlegendentry{Random Forest}
\addplot
coordinates {(FPR,0.31) (FNR,13.13)};

\addlegendentry{SVM}
\addplot
coordinates {(FPR,5.38) (FNR,17.11)};

\addlegendentry{Naive Bayes}
\addplot
coordinates {(FPR,3.63) (FNR,22.19)};

\end{axis}

\end{tikzpicture}
\end{adjustbox}
\caption{Left - No Interaction. Right - Interaction} \label{fig:M}
\end{figure*}
Stefan Pinnow
  • 29,535

2 Answers2

3

Here is a proposal using simplified code based on the groupplots library

\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=75.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[
            legend style={at={(-\pgfkeysvalueof{/pgfplots/group/horizontal sep}/2,-0.1)},
                    anchor=north,legend columns=-1},
            symbolic x coords={FPR,FNR},
            major tick length=0cm,
            xtick=data,
            ymin=0.0,
            enlarge x limits=0.5,
            enlarge y limits={upper,value=0.2},
            nodes near coords,
            ybar,
            every node near coord/.append style={rotate=90, anchor=west},
            bar width = 15pt,
        ]

        \addplot coordinates {(FPR,7.90) (FNR,14.0)};
        \addplot coordinates {(FPR,3.45) (FNR,13.75)};
        \addplot coordinates {(FPR,0.39) (FNR,13.63)};
        \addplot coordinates {(FPR,0.31) (FNR,13.13)};
        \addplot coordinates {(FPR,5.38) (FNR,17.11)};
        \addplot coordinates {(FPR,3.63) (FNR,22.19)};


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

\end{document}

enter image description here

BambOo
  • 8,801
  • 2
  • 20
  • 47
1

Thanks u so much sir for answering me. i found an easy solution

\begin{figure*}[h!]
\begin{adjustbox}{center}
\pgfplotsset{width=0.93\textwidth,height=0.4\textheight,compat=1.16}
\begin{tikzpicture}   
\begin{axis}[
    legend columns=-1,
    legend entries={KNN, Neural Networks, Decision Tree, Random  Forest, SVM, Naive Bayes},
    legend to name=named,
    legend style={at={(0.5,-0.09)},
    anchor=north,legend columns=-1},
    symbolic x coords={Precision,Recall,F1-Score,Accuracy},
    major tick length=0cm,
    xtick=data,
    ymin=75.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)};

\end{axis}
\end{tikzpicture}% NO EMPTY LINE HERE!!!! 
\begin{tikzpicture}  
\begin{axis}[
    legend style={at={(0.5,-0.09)},
    anchor=north,legend columns=-1},
    symbolic x coords={FPR,FNR},
    major tick length=0cm,
    xtick=data,
    ymin=0.0,
    enlarge x limits=0.5,
    enlarge y limits={upper,value=0.2},
    nodes near coords,
    ybar,
    every node near coord/.append style={rotate=90, anchor=west},
    bar width = 15pt,
]

\addplot 
coordinates {(FPR,7.90) (FNR,14.0)}; 

\addplot        
coordinates {(FPR,3.45) (FNR,13.75)};

\addplot 
coordinates {(FPR,0.39) (FNR,13.63)}; 

\addplot        
coordinates {(FPR,0.31) (FNR,13.13)};

\addplot 
coordinates {(FPR,5.38) (FNR,17.11)}; 

\addplot        
coordinates {(FPR,3.63) (FNR,22.19)};

\end{axis}
\end{tikzpicture}
\end{adjustbox}

\ref{named}
\caption{Left - No Interaction. Right - Interaction}
\label{fig:M}
\end{figure*}
  • 2
    It would be great if you could add an image of your result, so others have an easy way to see how your result looks like and can compare it to other answers. – Stefan Pinnow May 31 '20 at 14:23