2

I have a bar plot with three legends above each group, which I want to align. The first legend should be left, the second in the middle and the third right. I set each anchor to south west, south and south east respectively. All legends seems to have an offset to the left. Instead, I want the edge of the left legend to align with the left edge of the y-axis and the right edge of the right legend to align with the right edge of the y-axis. The middle legend should be exactly in the middle.

mwe

\documentclass[tikz,crop,margin=0.5cm]{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        height=8cm,
        width=0.9\textwidth,
        bar width=0.5cm,
        enlarge x limits=0.25,
        major x tick style=transparent,
        symbolic x coords={Question 1, Question 2, Question 3},
        xtick=data,
        ybar=2\pgflinewidth,
        ylabel={Count},
        ymajorgrids=true,
        ymax=20,
        ymin=0,
        ytick distance=2,
    ]
        \addplot[style={blue, fill=blue, mark=none, label=p1}]
            coordinates {(Question 1, 18) (Question 2, 16) (Question 3, 12)};
        \label{p1}

        \addplot[style={red, fill=red, mark=none}]
             coordinates {(Question 1, 2) (Question 2, 4) (Question 3, 8)};
        \label{p2}
    \end{axis}
    
    \node [draw, fill=white, anchor=south west] at (rel axis cs: 0, 1.05) {
        \shortstack[l] {
            \underline{Question 1} \\
            \ref{p1} Yea \\
            \ref{p2} Nay
        }
    };
    
    \node [draw, fill=white, anchor=south] at (rel axis cs: 0.5, 1.05) {
        \shortstack[l] {
            \underline{Question 2} \\
            \ref{p1} Right \\
            \ref{p2} Wrong
        }
    };
    
    \node [draw, fill=white, anchor=south east] at (rel axis cs: 1, 1.05) {
        \shortstack[l] {
            \underline{Question 3} \\
            \ref{p1} Agree \\
            \ref{p2} Disagree
        }
    };
\end{tikzpicture}

\end{document}

1 Answer 1

2

You could move the legends inside the axis environment and set clip = false.

\documentclass[tikz,crop,margin=0.5cm]{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        height=8cm,
        width=0.9\textwidth,
        bar width=0.5cm,
        enlarge x limits=0.25,
        major x tick style=transparent,
        symbolic x coords={Question 1, Question 2, Question 3},
        xtick=data,
        ybar=2\pgflinewidth,
        ylabel={Count},
        ymajorgrids=true,
        ymax=20,
        ymin=0,
        ytick distance=2,
        clip = false
    ]
        \addplot[style={blue, fill=blue, mark=none, label=p1}]
            coordinates {(Question 1, 18) (Question 2, 16) (Question 3, 12)};
        \label{p1}

        \addplot[style={red, fill=red, mark=none}]
             coordinates {(Question 1, 2) (Question 2, 4) (Question 3, 8)};
        \label{p2}
    
        \node [draw, fill=white, anchor=south west] at (rel axis cs: 0, 1.05) {
            \shortstack[l] {
                \underline{Question 1} \\
                \ref{p1} Yea \\
                \ref{p2} Nay
            }
        };
        
        \node [draw, fill=white, anchor=south] at (rel axis cs: 0.5, 1.05) {
            \shortstack[l] {
                \underline{Question 2} \\
                \ref{p1} Right \\
                \ref{p2} Wrong
            }
        };
        
        \node [draw, fill=white, anchor=south east] at (rel axis cs: 1, 1.05) {
            \shortstack[l] {
                \underline{Question 3} \\
                \ref{p1} Agree \\
                \ref{p2} Disagree
            }
        };
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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