[Tex/LaTex] Share legend between two tikpicture environments

legendpgfplotstikz-pgf

enter image description here

I have two tikzpictures with a bar-plot each in it. I want the two tikzpicture to share a common legend. The obvious way of clearing the legend in the first tikz and moving the legend of the second tikz into the middle of the two tikz-areas does not work.

The legends position seems to scale down the one tikzpicture. How can I make the one legend being centered between both tikzpictures without this weird scale-down effect.

Here is a MWE:

\documentclass{standalone}
\usepackage{times}
\usepackage{url}
\usepackage{latexsym}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots, pgfplotstable}    
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{nicefrac}
\pgfplotsset{grid style={dashed,gray}}
\usepackage{colortbl}
\usepackage{listings}
\usepgfplotslibrary{statistics}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage[justification=centering]{caption}


\begin{document}

%\begin{figure}
\centering

    \begin{tikzpicture}
       \pgfplotsset{grid style={dashed,gray}}
        \begin{axis}[
        ybar,
%        xlabel=Approachs,
        ylabel={Accuracy (\%)},
%       legend style={font=\tiny, at={(0.05,0.97)},anchor=north west,legend columns=1},
        legend style={font=\small, at={(-0.1,0.-0.2)},anchor=north west,legend columns=2},
        symbolic x coords={A, B},
        ymax=100,
        ymin=30.0,
        xtick=data,
        enlarge x limits=+0.5,
        ymajorgrids=true,   
        bar width=0.3cm,    
        xmajorgrids=true    ,
        ytick={30,40,...,100},                  
    ]      


\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
        (A, 83.4)
        (B, 89.0)       
};

\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\legend{}


\end{axis}
\end{tikzpicture}   

    \begin{tikzpicture}
       \pgfplotsset{grid style={dashed,gray}}
        \begin{axis}[
        ybar,
%        xlabel=Approachs,
        ylabel={Accuracy (\%)},
        legend style={font=\small, at={(-0.6,0.-0.2)},anchor=north west,legend columns=3},
        symbolic x coords={A, B},
        ymax=100,
        ymin=30.0,
        xtick=data,
        enlarge x limits=+0.5,
        ymajorgrids=true,   
        bar width=0.3cm,    
        xmajorgrids=true    ,
        ytick={30,40,...,100},                  
    ]      


\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
        (A, 83.4)
        (B, 89.0)       
};

\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};


\end{axis}
\end{tikzpicture}  


\end{document}

Best Answer

There is no scaling involved, and the effect you're seeing isn't really weird. A tikzpicture is in the end just a big box, that is placed on the baseline similar to the letter X. The bottom of the first tikzpicture is the bottom of the ticklabels on the axis, but the bottom of the second one is the bottom of the legend.

But anyways, just place both axis environments in the same tikzpicture, for example:

output of code

\documentclass{standalone}
\usepackage{pgfplotstable}    
\begin{document}
%\begin{figure}
%\centering

    \begin{tikzpicture}
       \pgfplotsset{grid style={dashed,gray}}
        \begin{axis}[
        name=ax1,
        ybar,
%        xlabel=Approachs,
        ylabel={Accuracy (\%)},
%       legend style={font=\tiny, at={(0.05,0.97)},anchor=north west,legend columns=1},
        legend style={font=\small, at={(-0.1,0.-0.2)},anchor=north west,legend columns=2},
        symbolic x coords={A, B},
        ymax=100,
        ymin=30.0,
        xtick=data,
        enlarge x limits=+0.5,
        ymajorgrids=true,   
        bar width=0.3cm,    
        xmajorgrids=true    ,
        ytick={30,40,...,100},                  
    ]      


\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
        (A, 83.4)
        (B, 89.0)       
};

\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\legend{}


\end{axis}

        \begin{axis}[
        at={(ax1.south east)},
        xshift=2cm,
        ybar,
%        xlabel=Approachs,
        ylabel={Accuracy (\%)},
        legend style={font=\small, at={(0,-0.1)},anchor=north east,legend columns=3},
        symbolic x coords={A, B},
        ymax=100,
        ymin=30.0,
        xtick=data,
        enlarge x limits=+0.5,
        ymajorgrids=true,   
        bar width=0.3cm,    
        xmajorgrids=true    ,
        ytick={30,40,...,100},                  
    ]      


\addlegendentry{X}
\addplot[blue,fill=blue] plot coordinates {
        (A, 83.4)
        (B, 89.0)       
};

\addlegendentry{Y}
\addplot[magenta,fill=magenta] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};

\addlegendentry{Y}
\addplot[orange,fill=orange] plot coordinates {
        (A, 85.2)
        (B, 90.6)       
};


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