两个图例并排

两个图例并排

我有以下代码来并排绘制两个图形并为每个图形绘制一个图例:

\documentclass[a4,9pt]{article}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage{tikz,pgfplots}
\usepackage{subfloat}
\usepackage{subfigure}
\usepackage[bottom=3cm,top=3cm,left=2cm,right=2cm]{geometry}

\begin{document}


\begin{figure}[!htb]
\centering
\subfigure[Figura da esquerda]{
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(0.5,-0.2)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 4) ($n=100$, 8) ($n=150$,10) ($n=200$,15)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 1) ($n=100$, 7) ($n=150$,7) ($n=200$, 12)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 9) ($n=150$,9) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,4) ($n=100$, 7) ($n=150$,9) ($n=200$, 13)}; %Solucao 4
                \legend{Sol.1,Sol.2,Sol.3,Sol.4}  
        \end{axis}  
    \end{tikzpicture}
}\qquad
\subfigure[Figura da direita]{
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(0.5,-0.2)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 3) ($n=100$, 4) ($n=150$,10) ($n=200$, 11)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 2) ($n=100$, 4) ($n=150$,4) ($n=200$, 4)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 12) ($n=150$,12) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,3) ($n=100$, 5) ($n=150$,10) ($n=200$,11)}; %Solucao 4
                \legend{Sol.1,Sol.2,Sol.3,Sol.4}  
        \end{axis}  
    \end{tikzpicture}
}
\caption{Número máximo de frentes de colheita}
\end{figure}

\end{document}

输出如下:

在此处输入图片描述

但是,我只想为这些图绘制一个图例(如下),如下所示:

在此处输入图片描述

有人能帮助我吗?

答案1

请参阅legend to name(第 269 页)pgfplots 文档)。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz,pgfplots}
\usepackage{subfigure}
\usepackage[bottom=3cm,top=3cm,left=2cm,right=2cm]{geometry}

\begin{document}


\begin{figure}[!htb]
\centering
\subfigure[Figura da esquerda]{
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(0.5,-0.2)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                legend to name={mylegend},
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 4) ($n=100$, 8) ($n=150$,10) ($n=200$,15)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 1) ($n=100$, 7) ($n=150$,7) ($n=200$, 12)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 9) ($n=150$,9) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,4) ($n=100$, 7) ($n=150$,9) ($n=200$, 13)}; %Solucao 4
                \legend{Sol.1,Sol.2,Sol.3,Sol.4}  
        \end{axis}  
    \end{tikzpicture}
}\qquad
\subfigure[Figura da direita]{
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(0.5,-0.2)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 3) ($n=100$, 4) ($n=150$,10) ($n=200$, 11)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 2) ($n=100$, 4) ($n=150$,4) ($n=200$, 4)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 12) ($n=150$,12) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,3) ($n=100$, 5) ($n=150$,10) ($n=200$,11)}; %Solucao 4
        \end{axis}  
    \end{tikzpicture}
}
\ref{mylegend}
\caption{Número máximo de frentes de colheita}
\end{figure}

\end{document}

在此处输入图片描述

答案2

以下代码可以完成您想要的操作。

您可以尝试 latexplots.com 或 latexplots.netlify.app,但是此工具是为 2D pgfplots 图形设计的,您可以为您的图形使用特定的块。

要做到这一点你必须:

1-在第一幅图像下方添加一个空白矩形并删除标签;2-移动第二幅图像的标签。3-重叠两幅图像(\hspace{-2cm})。4-可选,移动标题(\captionsetup[subfloat]{position=bottom,margin={-0.5cm,-3cm}}),在这种情况下使用 subfloat 而不是 subfigure,包 subfig。更多详细信息请参见:http://www.bakoma-tex.com/doc/latex/caption/caption-eng.pdf

\documentclass[a4,9pt]{article}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage{tikz,pgfplots}
%\usepackage{subfloat}
%\usepackage{subfigure}
\usepackage{subfig}
\usepackage[bottom=3cm,top=3cm,left=2cm,right=2cm]{geometry}

\begin{document}




\begin{figure}[!htb]
\centering
\captionsetup[subfloat]{position=bottom,margin={-0.5cm,-3cm}}%{oneside,margin={-0.5cm,-3cm}}
\subfloat[Figura da esquerda]{
\pgfplotsset{
every axis/.append style={
     extra description/.code={
     \draw[draw=white,
                 draw opacity=1,
                 fill opacity=1]
          (0.2,-.4)rectangle(0.8,-0.2);
},
},
}
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(0.5,-0.3)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 4) ($n=100$, 8) ($n=150$,10) ($n=200$,15)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 1) ($n=100$, 7) ($n=150$,7) ($n=200$, 12)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 9) ($n=150$,9) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,4) ($n=100$, 7) ($n=150$,9) ($n=200$, 13)}; %Solucao 4
               % \legend{Sol.1,Sol.2,Sol.3,Sol.4}  
        \end{axis}  
    \end{tikzpicture}
}\qquad
\hspace{-2cm}\subfloat[Figura da direita]{
    \begin{tikzpicture}  
        \begin{axis} [
                ybar=0pt,
                enlargelimits=0.2,
                width=0.45\textwidth, 
                height=0.35\textwidth,
                minor y tick num = 1,
                ymajorgrids,
                y label style={at={(axis description cs:0.02,0.5)}},
                bar width = 6pt,
                legend style={at={(-0.2,-0.3)},    
                    anchor=north,legend columns=4},     
                ylabel={Number of hours worked},
                symbolic x coords={$n=50$, $n=100$, $n=150$, $n=200$},  
                xtick=data,  
                ymax=15,
                ymin=0,
                %nodes near coords,  
                %nodes near coords align={above}
                ]  
                \addplot[fill=orange] coordinates 
                {($n=50$, 3) ($n=100$, 4) ($n=150$,10) ($n=200$, 11)}; %Solucao 1
                \addplot[fill=red] coordinates 
                {($n=50$, 2) ($n=100$, 4) ($n=150$,4) ($n=200$, 4)}; %Solucao 2
                \addplot[fill=blue] coordinates 
                {($n=50$, 4) ($n=100$, 12) ($n=150$,12) ($n=200$, 14)}; %Solucao 3
                \addplot[fill=violet] coordinates 
                {($n=50$,3) ($n=100$, 5) ($n=150$,10) ($n=200$,11)}; %Solucao 4
                \legend{Sol.1,Sol.2,Sol.3,Sol.4}  
        \end{axis}  
    \end{tikzpicture}
}
\caption{Número máximo de frentes de colheita}
\end{figure}

\end{document}

相关内容