1

I want to merge the two legends of this pgf plot, so that the second legend "This is legend two" is within the first legend box.

Here is the MWE:

\documentclass[
12pt,
a4paper,
]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[dvipsnames,table,xcdraw]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \pgfplotsset{
            show sum on top/.style={
                /pgfplots/scatter/@post marker code/.append code={%
                    \node[
                    at={(normalized axis cs:%
                        \pgfkeysvalueof{/data point/x},%
                        \pgfkeysvalueof{/data point/y})%
                    },
                    anchor=south,
                    color=black,
                    font=\footnotesize,
                    ]
                    {\pgfmathprintnumber[precision=2]{\pgfkeysvalueof{/data point/y}}};
                },
            },
        }
        \begin{axis}[
            width=0.7\textwidth,
            ybar stacked,
            bar width=20pt,
            nodes near coords={},
            ymin=0, ymax=16,
            legend pos=outer north east,
            legend cell align={left},
            legend style={font=\footnotesize,legend columns=1},
            tick label style={font=\footnotesize},
            xlabel style={font=\footnotesize},
            ylabel style={font=\footnotesize},
            ylabel={Total values},
            symbolic x coords={A, B, C, D, E}, 
            xtick=data
            ]
            \addplot+[fill=CornflowerBlue,  draw=black,ybar] plot coordinates {(A,3.34) (B,3.25) (C,1.95) (D,4.4468) (E,3.05)}; 
            \addplot+[fill=orange,          draw=black,ybar] plot coordinates {(A,0.43) (B,0.25) (C,1.06) (D,0.7987) (E,0.74)};
            \addplot+[fill=YellowGreen,     draw=black,ybar] plot coordinates {(A,0.97) (B,0.98) (C,2.11) (D,1.4551) (E,1.46)};
            \addplot+[fill=Goldenrod,       draw=black,ybar,show sum on top] plot coordinates {(A,0.6) (B,0.61) (C,1.73) (D,1.1238) (E,1.12)};
            \legend{This,is,the,Legend}
        \end{axis}
        
        \begin{axis}[
            width=0.7\textwidth,
            axis x line=none,
            axis y line=none,
            ymin=0, ymax=16,
            legend pos=north west,
            ytick=\empty,
            xtick=\empty,
            symbolic x coords={A, B, C, D, E},
            xtick=data
            ]
            \addplot [Lavender,mark=diamond*, only marks, draw=black,mark size=6pt]  plot coordinates {(A,0) (B,0) (C,15) (D,12) (E,12)};
            \addlegendentry{This is legend two}
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

1 Answer 1

2

The simplest should be to add a custom legend entry to the first plot. You can do this via \addlegendimage and \addlegendentry:

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
        \pgfplotsset{
            show sum on top/.style={
                /pgfplots/scatter/@post marker code/.append code={%
                    \node[
                        at={(normalized axis cs:%
                            \pgfkeysvalueof{/data point/x},%
                            \pgfkeysvalueof{/data point/y})%
                        },
                        anchor=south,
                        color=black,
                        font=\footnotesize,
                    ]
                    {\pgfmathprintnumber[precision=2]{\pgfkeysvalueof{/data point/y}}};
                },
            },
        }
        \begin{axis}[
            width=0.7\textwidth,
            ybar stacked,
            bar width=20pt,
            nodes near coords={},
            ymin=0, ymax=16,
            legend pos=outer north east,
            legend cell align={left},
            legend style={font=\footnotesize, legend columns=1},
            tick label style={font=\footnotesize},
            xlabel style={font=\footnotesize},
            ylabel style={font=\footnotesize},
            ylabel={Total values},
            symbolic x coords={A, B, C, D, E}, 
            xtick=data
        ]
            \addplot+[fill=CornflowerBlue, draw=black, ybar] plot coordinates {(A,3.34) (B,3.25) (C,1.95) (D,4.4468) (E,3.05)}; 
            \addplot+[fill=orange, draw=black, ybar] plot coordinates {(A,0.43) (B,0.25) (C,1.06) (D,0.7987) (E,0.74)};
            \addplot+[fill=YellowGreen, draw=black, ybar] plot coordinates {(A,0.97) (B,0.98) (C,2.11) (D,1.4551) (E,1.46)};
            \addplot+[fill=Goldenrod, draw=black, ybar, show sum on top] plot coordinates {(A,0.6) (B,0.61) (C,1.73) (D,1.1238) (E,1.12)};
            \legend{This,is,the,Legend}

            \addlegendimage{Lavender, mark=diamond*, only marks, draw=black, mark size=6pt, yshift=-.5em}
            \addlegendentry{This is legend two}
        \end{axis}

        \begin{axis}[
            width=0.7\textwidth,
            axis x line=none,
            axis y line=none,
            ymin=0, ymax=16,
            legend pos=north west,
            ytick=\empty,
            xtick=\empty,
            symbolic x coords={A, B, C, D, E},
            xtick=data
        ]
            \addplot[Lavender, mark=diamond*, only marks, draw=black, mark size=6pt] plot coordinates {(A,0) (B,0) (C,15) (D,12) (E,12)};
        \end{axis}
    \end{tikzpicture}
\end{document}
2
  • Thanks, it seems like a "workaround", but it does the trick. Jan 30 at 15:09
  • 1
    @FantasticMsFox As far as I know you can't merge two legends from different axis environments. You could try to place all the plots in one single axis environment, but I am currently unsure how to tell PGFPlots that the last plot should not be stacked. Jan 30 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .