PGF 图中分层刻度标签

PGF 图中分层刻度标签

我正在寻找一种在 tikz/pdfplots 中创建分层刻度标签的方法 - 就像在本示例图片中一样: 在此处输入图片描述

前三组描述实验 A 的结果,参数分别为 0、4 和 8,后三组对实验 B 执行相同操作,依此类推。

但是,我不知道如何实现这一点,到目前为止在网上找不到任何信息。有人能提示或解决这个问题吗?非常感谢阅读这篇文章!

这是我目前的代码,我没有想出比将参数写在括号中并一遍又一遍重复实验名称更好的解决方案……

\documentclass[preview]{standalone}

\usepackage{pgfplots,pgfplotstable} % plots
\tikzset{every picture/.style={/utils/exec={\sffamily}}} % font for plots
\tikzset{every picture/.style={font=\small}} % font size for plots
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=8cm,
        ymax=93,
        ymin=0,
        ybar,
        bar width=4pt,
        symbolic x coords={A (0), A (4), A (8), B (0), B (4), B (8), C (0), C (4), C (8)},
        xtick=data,
        x tick label style={rotate=45,anchor=east}]

    \addplot+[ybar] plot coordinates {
        (A (0),61.682)
        (A (4),63.104)
        (A (8),60.82)
        
        (B (0),65.454)
        (B (4),65.396)
        (B (8),63.69)
        
        (C (0),77.73)
        (C (4),75.756)
        (C (8),70.29)};

    \addplot+[ybar] plot coordinates {
        (A (0),35.632)
        (A (4),34.65)
        (A (8),36.76) 
        
        (B (0),32.596)
        (B (4),32.47)
        (B (8),34.14)
        
        (C (0),21.58)
        (C (4),23.364)
        (C (8),28.87)};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

为了获得正确的 xticks,你可以结合xtickxticklabels。之后,可以使用节点来定位组标题AC. 为此,必须关闭剪辑。

\documentclass{standalone}

\usepackage{pgfplots,pgfplotstable} % plots
\tikzset{every picture/.style={/utils/exec={\sffamily}}} % font for plots
\tikzset{every picture/.style={font=\small}} % font size for plots
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=8cm,
        ymax=93,
        ymin=0,
        ybar,
        bar width=8pt,
        % x ticks explicitly formatted
        xtick={1,2,3,5,6,7,9,10,11},
        xticklabels={$0$,$4$,$8$,$0$,$4$,$8$,$0$,$4$,$8$},
        % turn of clipping
        clip=false
    ]

    \addplot+[ybar] plot 
        coordinates {
            (1,61.682)
            (2,63.104)
            (3,60.82)

            (5,65.454)
            (6,65.396)
            (7,63.69)

            (9,77.73)
            (10,75.756)
            (11,70.29)
        };

    \addplot+[ybar] plot
        coordinates {
            (1,35.632)
            (2,34.65)
            (3,36.76) 

            (5,32.596)
            (6,32.47)
            (7,34.14)

            (9,21.58)
            (10,23.364)
            (11,28.87)
        };

    \node[font=\bfseries] at (xticklabel cs:.1675,10pt) {A};
    \node[font=\bfseries] at (xticklabel cs:.5000,10pt) {B};
    \node[font=\bfseries] at (xticklabel cs:.8325,10pt) {C};
\end{axis}
\end{tikzpicture}
\end{document}

输出

相关内容