如何将两个条形图并排放置

如何将两个条形图并排放置

此代码片段中有两个 tikz 条形图。我可以一个接一个地显示条形图,但当我使用 subfigure/subfloat 时,会出现错误

\documentclass[preprint,12pt,3p]{elsarticle}
\usepackage{graphicx}
\usepackage{float}

\usepackage{epstopdf}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.10}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{figure}[ht!]
    \centering
    \subfloat{
        \begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymin=0,
        width  = 12cm,
        height = 5cm,
        bar width=5pt,
        ylabel={$\mathcal{J}$ of WM},
        nodes near coords,
        xticklabel style={rotate=90},
        xtick = data,
        table/header=false,
        table/row sep=\\,
        xticklabels from table={\footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          }{[index]0},
        enlarge y limits={value=0.2,upper}
      %  legend pos=north west
    ]
     \legend{1\%, 5\%}
    \addplot table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \pgfplotsinvokeforeach{0,3,4,7,8,11}{\coordinate(l#1)at(axis cs:#1,0);}
    \end{axis}
    \coordinate(bbs)at(current bounding box.south);
    \foreach[count=\i,evaluate={\s=int(4*\i-1)},evaluate={\e=int(4*(\i-1)}] \text in {Axial,Coronial,Sagittarial} 
      \draw[decorate,decoration=brace]([xshift=8pt]l\s|-bbs)--node[below=5pt]{\text}([xshift=-8pt]l\e|-bbs);
\end{tikzpicture}
    }
    % A blank line here like a new paragraph so next picture is placed below
    \subfloat{
         \begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymin=0,
        width  = 12cm,
        height = 5cm,
        bar width=5pt,
        ylabel={$\mathcal{J}$ of WM},
        nodes near coords,
        xticklabel style={rotate=90},
        xtick = data,
        table/header=false,
        table/row sep=\\,
        xticklabels from table={\footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          }{[index]0},
        enlarge y limits={value=0.2,upper}
      %  legend pos=north west
    ]
     \legend{1\%, 5\%}
    \addplot table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \pgfplotsinvokeforeach{0,3,4,7,8,11}{\coordinate(l#1)at(axis cs:#1,0);}
    \end{axis}
    \coordinate(bbs)at(current bounding box.south);
    \foreach[count=\i,evaluate={\s=int(4*\i-1)},evaluate={\e=int(4*(\i-1)}] \text in {Axial,Coronial,Sagittarial} 
      \draw[decorate,decoration=brace]([xshift=8pt]l\s|-bbs)--node[below=5pt]{\text}([xshift=-8pt]l\e|-bbs);
\end{tikzpicture}
    }

    \caption{Accuracies Comparision}
    \end{figure}
\end{document}

答案1

你漏掉了一个括号(2 处)

\foreach[count=\i,evaluate={\s=int(4*\i-1)},evaluate={\e=int(4*(\i-1)) %<-- this one, you have only one of them.

您忘记加载subfig提供\subfloat命令的包。话虽如此,您可以使用标准方法\resizebox

\documentclass[preprint,12pt,3p]{elsarticle}
\usepackage{graphicx}
\usepackage{float}

\usepackage{epstopdf}
\usepackage{caption}
\usepackage{subfig}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.10}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{figure}[ht!]
    \centering
    \subfloat[][Sub figure first]{\resizebox{0.5\textwidth}{!}{
        \begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymin=0,
        width  = 12cm,
        height = 5cm,
        bar width=5pt,
        ylabel={$\mathcal{J}$ of WM},
        nodes near coords,
        xticklabel style={rotate=90},
        xtick = data,
        table/header=false,
        table/row sep=\\,
        xticklabels from table={\footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          }{[index]0},
        enlarge y limits={value=0.2,upper}
      %  legend pos=north west
    ]
     \legend{1\%, 5\%}
    \addplot table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \pgfplotsinvokeforeach{0,3,4,7,8,11}{\coordinate(l#1)at(axis cs:#1,0);}
    \end{axis}
    \coordinate(bbs)at(current bounding box.south);
    \foreach[count=\i,evaluate={\s=int(4*\i-1)},evaluate={\e=int(4*(\i-1))}] \text in {Axial,Coronial,Sagittarial}
      \draw[decorate,decoration=brace]([xshift=8pt]l\s|-bbs)--node[below=5pt]{\text}([xshift=-8pt]l\e|-bbs);
\end{tikzpicture}
    }}
    % A blank line here like a new paragraph so next picture is placed below
    \subfloat[][Sub figure second]{\resizebox{0.5\textwidth}{!}{
         \begin{tikzpicture}
    \begin{axis}[
        ybar,
        ymin=0,
        width  = 12cm,
        height = 5cm,
        bar width=5pt,
        ylabel={$\mathcal{J}$ of WM},
        nodes near coords,
        xticklabel style={rotate=90},
        xtick = data,
        table/header=false,
        table/row sep=\\,
        xticklabels from table={\footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          }{[index]0},
        enlarge y limits={value=0.2,upper}
      %  legend pos=north west
    ]
     \legend{1\%, 5\%}
    \addplot table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
    \pgfplotsinvokeforeach{0,3,4,7,8,11}{\coordinate(l#1)at(axis cs:#1,0);}
    \end{axis}
    \coordinate(bbs)at(current bounding box.south);
    \foreach[count=\i,evaluate={\s=int(4*\i-1)},evaluate={\e=int(4*(\i-1))}] \text in {Axial,Coronial,Sagittarial}
      \draw[decorate,decoration=brace]([xshift=8pt]l\s|-bbs)--node[below=5pt]{\text}([xshift=-8pt]l\e|-bbs);
\end{tikzpicture}
    }}
    \caption{Accuracies Comparision}
    \end{figure}
\end{document}

在此处输入图片描述

相关内容