使用 pgfplot 整理条形图

使用 pgfplot 整理条形图

我有一个具有三个层级(颜色+内组+外组)的条形图:

原始图像

该图看起来超载了,但我不知道如何以更好的方式拆分 x 轴标签。一个可能的解决方案可能是这个草图:

可能的修改

由于我还没有想出一个适合我的工作流程,这个最小的例子仍然有些混乱。我使用 matlab2tikz 作为开始,然后手动做了一些 tikzmodifications:

\documentclass{scrartcl}
\usepackage{pgfplots} % Import der Plots aus Matlab
\usepackage{tikz} % Import der Plots aus Matlab
\newlength{\fheight}
\newlength{\fwidth}
\pgfplotsset{compat=1.3}


\usepackage{amsmath,amsthm, amssymb, latexsym}

\begin{document}
\setlength\fheight{0.2\columnwidth}
\setlength\fwidth{0.42\columnwidth}

\definecolor{mycolor1}{rgb}{0.00000,0.00000,0.56250}%
\definecolor{mycolor2}{rgb}{0.00000,0.18750,1.00000}%
\definecolor{mycolor3}{rgb}{0.00000,0.87500,1.00000}%
\definecolor{mycolor4}{rgb}{0.56250,1.00000,0.43750}%
\definecolor{mycolor5}{rgb}{1.00000,0.81250,0.00000}%
% 
\begin{tikzpicture}[baseline, trim axis left, trim axis right]
  \newlength{\myBarWidth}
  \setlength{\myBarWidth}{0.011\fwidth}
  \newlength{\myBarShift}
  \setlength{\myBarShift}{0.011\fwidth}
  \begin{axis}[%
  legend columns=4,
    width=\fwidth,
    height=\fheight,
    area legend,
    scale only axis,
    xmin=0.25,
    xmax=11.75,
    xtick={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
    xticklabels={
      {$\begin{array}{c}
          10\\
          \phantom{...}
        \end{array}$
      },
      {$\begin{array}{c}
          20\\
          \text{proposed},\pi_{\text{Th}\phantom{...}}
        \end{array}$
      },
      {$\begin{array}{c}
          30\\
          \phantom{...}
        \end{array}$
      },
      {},
      {$\begin{array}{c}
          10\\
          \phantom{...}
        \end{array}$
      },
      {$\begin{array}{c}
          20\\
          \text{proposed},\kappa_{\text{Th}}
        \end{array}$
      },
      {$\begin{array}{c}
          30\\
          \phantom{...}
        \end{array}$
      },
      {},
      {$\begin{array}{c}10\\\phantom{...}\end{array}$},
      {$\begin{array}{c}20\\\text{DOA-based}\end{array}$},
      {$\begin{array}{c}30\\\phantom{...}\end{array}$}
    },
    ymin=0,
    ymax=100,
    ytick={0, 20, 40, 60, 80, 100},
    ylabel={$\text{Accuracy}$},
    y label style={at={(axis description cs:-0.08,.5)},rotate=0,anchor=south},
legend style={at={(0.5,-0.35)},anchor=north,draw=black,fill=white,legend cell align=left,
    /tikz/column 2/.style={column sep=5pt},
    /tikz/column 4/.style={column sep=5pt},
    /tikz/column 6/.style={column sep=5pt}
  }
    ]
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-3\myBarShift,
    draw=black,
    fill=mycolor1
    ]
    plot table[row sep=crcr] {%
      1  100\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9  100\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 0$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-2\myBarShift,
    draw=black,
    fill=mycolor2
    ]
    plot table[row sep=crcr] {%
      1 99.8\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9 32.8\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 1$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-1\myBarShift,
    draw=black,
    fill=mycolor3
    ]
    plot table[row sep=crcr] {%
      1  98.4\\  2  99.4\\  3  90.6\\  4  0\\
      5  98.2\\  6  97.4\\  7  90.4\\  8  0\\
      9  14.4\\ 10    79\\ 11  98.8\\
    };
\addlegendentry{$K = 2$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=0\myBarShift,
    draw=black,
    fill=mycolor4
    ]
    plot table[row sep=crcr] {%
      1  87.4\\  2  99.4\\  3  95.8\\  4  0\\
      5  92.8\\  6  98.4\\  7  91.6\\  8  0\\
      9     1\\ 10  27.2\\ 11  79.6\\
    };
\addlegendentry{$K = 3$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=1\myBarShift,
    draw=black,
    fill=mycolor5
    ]
    plot table[row sep=crcr] {%
      1  63.8\\  2  98.2\\  3  93.6\\  4  0\\
      5    80\\  6  94.4\\  7  85.4\\  8  0\\
      9     0\\ 10  34.4\\ 11  60.6\\
    };
\addlegendentry{$K = 4$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=2\myBarShift,
    draw=black,
    fill=red!75!orange
    ]
    plot table[row sep=crcr] {%
      1  31.8\\  2    94\\  3    92\\  4  0\\
      5  56.6\\  6  81.8\\  7    83\\  8  0\\
      9   0.2\\ 10  40.6\\ 11  52.2\\
    };
\addlegendentry{$K = 5$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=3\myBarShift,
    draw=black,
    fill=black!50!red
    ]
    plot table[row sep=crcr] {%
      1     7\\  2  57.2\\  3  56.6\\  4  0\\
      5  31.6\\  6    39\\  7  32.4\\  8  0\\
      9     0\\ 10     0\\ 11     0\\
    };
\addlegendentry{$K = 6$};
  \end{axis}
\end{tikzpicture}%

\end{document}

粘贴箱

答案1

修改内容:

  1. font=\scriptsize以图例风格添加。
  2. 将图例移至顶部at={(rel axis cs: 0.5,1.05)}
  3. 变成widthwidth=1.5\fwidth,
  4. 全部删除\phantom{...}
  5. 添加\\[-2.5ex] \underbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-0.5ex]

有了这些,我们得到

在此处输入图片描述

代码:

\documentclass{scrartcl}
\usepackage{pgfplots} % Import der Plots aus Matlab
%% \usepackage{tikz} % not needed pgfplots loads this too
\newlength{\fheight}
\newlength{\fwidth}
\pgfplotsset{compat=1.3}


\usepackage{amsmath,amsthm, amssymb, latexsym}

\begin{document}
\setlength\fheight{0.2\columnwidth}
\setlength\fwidth{0.42\columnwidth}

\definecolor{mycolor1}{rgb}{0.00000,0.00000,0.56250}%
\definecolor{mycolor2}{rgb}{0.00000,0.18750,1.00000}%
\definecolor{mycolor3}{rgb}{0.00000,0.87500,1.00000}%
\definecolor{mycolor4}{rgb}{0.56250,1.00000,0.43750}%
\definecolor{mycolor5}{rgb}{1.00000,0.81250,0.00000}%
%
\begin{tikzpicture}[baseline, trim axis left, trim axis right]
  \newlength{\myBarWidth}
  \setlength{\myBarWidth}{0.011\fwidth}
  \newlength{\myBarShift}
  \setlength{\myBarShift}{0.011\fwidth}
  \begin{axis}[%
  legend columns=4,
    width=1.5\fwidth,
    height=\fheight,
    area legend,
    scale only axis,
    xmin=0.25,
    xmax=11.75,
    xtick={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
    xticklabels={
      {$\begin{array}{c}
          10
        \end{array}$
      },
      {$\begin{array}{c}
          20\\[-2.5ex]
          \underbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-0.5ex]
          \text{proposed},\pi_{\text{Th}}
        \end{array}$
      },
      {$\begin{array}{c}
          30
        \end{array}$
      },
      {},
      {$\begin{array}{c}
          10
        \end{array}$
      },
      {$\begin{array}{c}
          20\\[-2.5ex]
          \underbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-0.5ex]
          \text{proposed},\kappa_{\text{Th}}
        \end{array}$
      },
      {$\begin{array}{c}
          30
        \end{array}$
      },
      {},
      {$\begin{array}{c}10\end{array}$},
      {$\begin{array}{c}20\\[-2.5ex]
          \underbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-0.5ex]\text{DOA-based}\end{array}$},
      {$\begin{array}{c}30\end{array}$}
    },
    ymin=0,
    ymax=100,
    ytick={0, 20, 40, 60, 80, 100},
    ylabel={$\text{Accuracy}$},
    y label style={at={(axis description cs:-0.08,.5)},rotate=0,anchor=south},
legend style={at={(rel axis cs: 0.5,1.05)},anchor=south,draw=black,font=\scriptsize,fill=white,legend cell align=left,
    /tikz/column 2/.style={column sep=5pt},
    /tikz/column 4/.style={column sep=5pt},
    /tikz/column 6/.style={column sep=5pt}
  }
    ]
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-3\myBarShift,
    draw=black,
    fill=mycolor1
    ]
    plot table[row sep=crcr] {%
      1  100\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9  100\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 0$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-2\myBarShift,
    draw=black,
    fill=mycolor2
    ]
    plot table[row sep=crcr] {%
      1 99.8\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9 32.8\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 1$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-1\myBarShift,
    draw=black,
    fill=mycolor3
    ]
    plot table[row sep=crcr] {%
      1  98.4\\  2  99.4\\  3  90.6\\  4  0\\
      5  98.2\\  6  97.4\\  7  90.4\\  8  0\\
      9  14.4\\ 10    79\\ 11  98.8\\
    };
\addlegendentry{$K = 2$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=0\myBarShift,
    draw=black,
    fill=mycolor4
    ]
    plot table[row sep=crcr] {%
      1  87.4\\  2  99.4\\  3  95.8\\  4  0\\
      5  92.8\\  6  98.4\\  7  91.6\\  8  0\\
      9     1\\ 10  27.2\\ 11  79.6\\
    };
\addlegendentry{$K = 3$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=1\myBarShift,
    draw=black,
    fill=mycolor5
    ]
    plot table[row sep=crcr] {%
      1  63.8\\  2  98.2\\  3  93.6\\  4  0\\
      5    80\\  6  94.4\\  7  85.4\\  8  0\\
      9     0\\ 10  34.4\\ 11  60.6\\
    };
\addlegendentry{$K = 4$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=2\myBarShift,
    draw=black,
    fill=red!75!orange
    ]
    plot table[row sep=crcr] {%
      1  31.8\\  2    94\\  3    92\\  4  0\\
      5  56.6\\  6  81.8\\  7    83\\  8  0\\
      9   0.2\\ 10  40.6\\ 11  52.2\\
    };
\addlegendentry{$K = 5$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=3\myBarShift,
    draw=black,
    fill=black!50!red
    ]
    plot table[row sep=crcr] {%
      1     7\\  2  57.2\\  3  56.6\\  4  0\\
      5  31.6\\  6    39\\  7  32.4\\  8  0\\
      9     0\\ 10     0\\ 11     0\\
    };
\addlegendentry{$K = 6$};
  \end{axis}
\end{tikzpicture}%

\end{document}

这是另一种布局:

\documentclass{scrartcl}
\usepackage{pgfplots} % Import der Plots aus Matlab
%% \usepackage{tikz} % not needed pgfplots loads this too
\newlength{\fheight}
\newlength{\fwidth}
\pgfplotsset{compat=1.3}


\usepackage{amsmath,amsthm, amssymb, latexsym}

\begin{document}
\setlength\fheight{0.2\columnwidth}
\setlength\fwidth{0.42\columnwidth}

\definecolor{mycolor1}{rgb}{0.00000,0.00000,0.56250}%
\definecolor{mycolor2}{rgb}{0.00000,0.18750,1.00000}%
\definecolor{mycolor3}{rgb}{0.00000,0.87500,1.00000}%
\definecolor{mycolor4}{rgb}{0.56250,1.00000,0.43750}%
\definecolor{mycolor5}{rgb}{1.00000,0.81250,0.00000}%
%
\begin{tikzpicture}[baseline, trim axis left, trim axis right]
  \newlength{\myBarWidth}
  \setlength{\myBarWidth}{0.011\fwidth}
  \newlength{\myBarShift}
  \setlength{\myBarShift}{0.011\fwidth}
  \begin{axis}[%
  legend columns=4,
    width=1.5\fwidth,
    height=\fheight,
    area legend,
    scale only axis,
    clip=false,
    xmin=0.25,
    xmax=11.75,
    xtick={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
    xticklabels={
      {$\begin{array}{c}
          10
        \end{array}$
      },
      {$\begin{array}{c}
          20
        \end{array}$
      },
      {$\begin{array}{c}
          30
        \end{array}$
      },
      {},
      {$\begin{array}{c}
          10
        \end{array}$
      },
      {$\begin{array}{c}
          20
        \end{array}$
      },
      {$\begin{array}{c}
          30
        \end{array}$
      },
      {},
      {$\begin{array}{c}10\end{array}$},
      {$\begin{array}{c}20\end{array}$},
      {$\begin{array}{c}30\end{array}$}
    },
    ymin=0,
    ymax=100,
    ytick={0, 20, 40, 60, 80, 100},
    ylabel={$\text{Accuracy}$},
    y label style={at={(axis description cs:-0.08,.5)},rotate=0,anchor=south},
legend style={at={(rel axis cs: 0.5,-0.3)},anchor=north,draw=black,font=\scriptsize,fill=white,legend cell align=left,
    /tikz/column 2/.style={column sep=5pt},
    /tikz/column 4/.style={column sep=5pt},
    /tikz/column 6/.style={column sep=5pt}
  }
    ]
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-3\myBarShift,
    draw=black,
    fill=mycolor1
    ]
    plot table[row sep=crcr] {%
      1  100\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9  100\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 0$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-2\myBarShift,
    draw=black,
    fill=mycolor2
    ]
    plot table[row sep=crcr] {%
      1 99.8\\  2  100\\  3  100\\  4  0\\
      5  100\\  6  100\\  7  100\\  8  0\\
      9 32.8\\ 10  100\\ 11  100\\
    };
\addlegendentry{$K = 1$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=-1\myBarShift,
    draw=black,
    fill=mycolor3
    ]
    plot table[row sep=crcr] {%
      1  98.4\\  2  99.4\\  3  90.6\\  4  0\\
      5  98.2\\  6  97.4\\  7  90.4\\  8  0\\
      9  14.4\\ 10    79\\ 11  98.8\\
    };
\addlegendentry{$K = 2$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=0\myBarShift,
    draw=black,
    fill=mycolor4
    ]
    plot table[row sep=crcr] {%
      1  87.4\\  2  99.4\\  3  95.8\\  4  0\\
      5  92.8\\  6  98.4\\  7  91.6\\  8  0\\
      9     1\\ 10  27.2\\ 11  79.6\\
    };
\addlegendentry{$K = 3$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=1\myBarShift,
    draw=black,
    fill=mycolor5
    ]
    plot table[row sep=crcr] {%
      1  63.8\\  2  98.2\\  3  93.6\\  4  0\\
      5    80\\  6  94.4\\  7  85.4\\  8  0\\
      9     0\\ 10  34.4\\ 11  60.6\\
    };
\addlegendentry{$K = 4$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=2\myBarShift,
    draw=black,
    fill=red!75!orange
    ]
    plot table[row sep=crcr] {%
      1  31.8\\  2    94\\  3    92\\  4  0\\
      5  56.6\\  6  81.8\\  7    83\\  8  0\\
      9   0.2\\ 10  40.6\\ 11  52.2\\
    };
\addlegendentry{$K = 5$};
    \addplot[
    ybar,
    bar width=\myBarWidth,
    bar shift=3\myBarShift,
    draw=black,
    fill=black!50!red
    ]
    plot table[row sep=crcr] {%
      1     7\\  2  57.2\\  3  56.6\\  4  0\\
      5  31.6\\  6    39\\  7  32.4\\  8  0\\
      9     0\\ 10     0\\ 11     0\\
    };
\addlegendentry{$K = 6$};
  \node[anchor=south] at (axis cs: 2,105) {$\begin{array}{c}
          \text{proposed},\pi_{\text{Th}}\\[-0.7ex]
          \overbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-2.25ex]
        \end{array}$};
  \node[anchor=south] at (axis cs: 6,105) {
          $\begin{array}{c}
          \text{proposed},\kappa_{\text{Th}}\\[-0.7ex]
          \overbrace{\hphantom{\text{roposed},\kappa_{\text{Th}}}}\\[-2.25ex]
        \end{array}$};
  \node[anchor=south] at (axis cs: 10,105) {
          $\begin{array}{c}
          \text{\text{DOA-based}}\\[-0.7ex]
          \overbrace{\hphantom{\text{roposed},\pi_{\text{Th}}}}\\[-2.25ex]
        \end{array}$};
  \end{axis}
\end{tikzpicture}%

\end{document}

在此处输入图片描述

相关内容