如何正确对齐堆积条形图中的条形图?

如何正确对齐堆积条形图中的条形图?

我正在研究此乳胶代码以正确适应边界。我想在每个条形图的顶部包含百分比,并且图例必须只是一个矩形。另外,有人可以帮我在这个代码中使用模式吗?

代码:

\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=12pt,
        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 
          % wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          % \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\
          }{[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\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\};
    \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}
      \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=12pt,
        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 
          % wells\\\footnotesize lemput\\\footnotesize wang\\\footnotesize proposed\\
          % \footnotesize
          wells\\\footnotesize lemput\\\footnotesize wang\\
          }{[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\\};
    \addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\};
    \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}
      \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

改编

  • 简化示例以达到最小化

    • 删除不需要的包
    • 删除了第二个地块
  • 仅用于xticklabel style={font=\footnotesize}设置一次尺寸

  • 在这里使用正确的数字:\pgfplotsinvokeforeach{0,2,3,5}{...}

    • 这些数字对应于图中的 x 位置
  • 改变循环中的计算foreach,以获得正确的大小 3:

    evaluate={\s=int(3*\i-1)}, evaluate={\e=int(3*(\i-1))}

  • 可以使用以下选项添加百分号:

    nodes near coords*={\footnotesize \pgfmathprintnumber{\pgfplotspointmeta}\%}

    • 但我不推荐这样做,因为它需要更多空间,而且是不必要的信息

代码

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

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            ybar,
            ymin=0,
            width  = 12cm,
            height = 5cm,
            bar width=12pt,
            ylabel={$\mathcal{J}$ of WM},
            nodes near coords*={\footnotesize \pgfmathprintnumber{\pgfplotspointmeta}\%},
            xticklabel style={rotate=90, font=\footnotesize},
            xtick = data,
            table/header=false,
            table/row sep=\\,
            xticklabels from table={%
                wells\\
                lemput\\
                wang\\
                wells\\
                lemput\\
                wang\\
            }{[index]0},
            enlarge y limits={value=0.2,upper}
            %  legend pos=north west
        ]
        
            \legend{1\%, 5\%}
            \addplot[pattern=horizontal lines light blue] table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\};
            \addplot[pattern=north east lines, pattern color=red] table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\};
            \pgfplotsinvokeforeach{0,2,3,5}{
                \coordinate(l#1) at (axis cs:#1,0);
            }
        \end{axis}
        \coordinate (bbs) at (current bounding box.south);
        \foreach[count=\i, evaluate={\s=int(3*\i-1)}, evaluate={\e=int(3*(\i-1))}] \text in {Axial,Coronial}
            \draw[decorate,decoration=brace] ([xshift=8pt] l\s|-bbs) -- node[below=5pt] {\text} ([xshift=-8pt] l\e|-bbs);
    \end{tikzpicture}
\end{figure}

\end{document}

结果

在此处输入图片描述

相关内容