条形图布局

条形图布局

我有以下条形图: 在此处输入图片描述

我想在条形图中更改以下内容:

  • 轴上的“1”和“2”组合在一起,即靠得更近,称为组 A1。组名“A1”应列在条形图上方。“A2”也是如此
  • '1' 和 '2'(以及 '3' 和 '4')之间的间距应该较小,但组 'A1' 和 'A2' 之间的间距必须较大,这样才能看出我们正在谈论两个不同的组。颜色可能是黑色。
  • 我想画一条水平线。

这就是我要的: 在此处输入图片描述

一位 MWE 表示:

\documentclass{report}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfplotsset{every axis/.append style={
        scaled y ticks = false, 
        scaled x ticks = false, 
        y tick label style={/pgf/number format/fixed},
        x tick label style={/pgf/number format/fixed}
    }
}

\begin{document}


\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
legend entries={First,Second, Total},
legend to name=legendLF,
legend columns = 3,
height=50mm,
width= 15cm,
     x tick label style={/pgf/number format/1000 sep=},
    ylabel={Value},
        xlabel= {Position},
                xtick={1,2,3,4},
                    enlargelimits=0.1,
       legend style={
          at={(0.75,-0.2)},
          anchor=north,
        },
        nodes near coords,
        every node near coord/.append style={font=\tiny},
        ybar=0pt,
        bar width=10pt
      ]
     \addplot[gray!20!gray,fill=gray!60!gray]
        coordinates {(01,46) (02,51)
          (03,51) (04,58) };
        \addplot[darkgray!20!darkgray,fill=darkgray!80!darkgray]
        coordinates {(01,47) (02,53)
          (03,56) (04,72)};
      \addplot[black!20!black,fill=black!80!black]
        coordinates {(01,60) (02,56)
          (03,58) (04,69) };
    \end{axis}
\end{tikzpicture}
\ref{legendLF}
\end{figure}
\end{document}

答案1

首先legend to name启用图例导出模式而不是绘制图例,在这种情况下您无法用控制图例的位置legend style={at={(0.5,-0.4)}}

将节点放置在

\node[red] at (rel axis cs:0.18,0.7) {$A_1$}; 
\node[red] at (rel axis cs:0.78,0.7) {$A_2$};

画红线

\draw[red,thick](yticklabel* cs:0.53)--({yticklabel* cs:0.53} -|{xticklabel* cs:1});

代码

\documentclass{report}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfplotsset{every axis/.append style={
        scaled y ticks = false, 
        scaled x ticks = false, 
        y tick label style={/pgf/number format/fixed},
        x tick label style={/pgf/number format/fixed}
    }
}

\begin{document}


\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
legend entries={First,Second, Total},
%legend to name=legendLF,
legend style={at={(0.5,-0.4)},anchor=north,legend columns = 3},
height=50mm,
width= 15cm,
x tick label style={/pgf/number format/1000 sep=},
ylabel={Value},
xlabel= {Position},
symbolic x coords={1,2, ,3,4},
xtick=data,
enlargelimits=0.1,
nodes near coords,
every node near coord/.append style={font=\tiny},
ybar=0pt,
bar width=10pt]

     \addplot[gray!20!gray,fill=gray!60!gray]
        coordinates {(1,46) (2,51) (3,51) (4,58)};
     \addplot[darkgray!20!darkgray,fill=darkgray!80!darkgray]
        coordinates {(1,47) (2,53) (3,56) (4,72)};
     \addplot[black!20!black,fill=black!80!black]
        coordinates {(1,60) (2,56) (3,58) (4,69)};

     \node[red] at (rel axis cs:0.18,0.7) {$A_1$}; 
     \node[red] at (rel axis cs:0.78,0.7) {$A_2$};
     \draw[red,thick](yticklabel* cs:0.53)--({yticklabel* cs:0.53} -|{xticklabel* cs:1});
\end{axis}
\end{tikzpicture}
%\ref{legendLF}
\end{figure}
\end{document}

输出

在此处输入图片描述

相关内容