如何使用表格附件创建分组直方图

如何使用表格附件创建分组直方图

我正在尝试使用 latex 编写以下图表。有人能做到吗?(网格设置和颜色并不重要)

在此处输入图片描述

表格如下

\begin{filecontents}{table.dat}
T   L   F   LF
400 0.952   0.932   0.972
500 0.969   0.987   0.988
600 0.969   0.997   0.997
700 0.917   0.950   0.976
\end{filecontents}

答案1

这里有一个解决方法tabularx,我们将直方图放在\multicolumn第一行,然后完成其他行。

注意这里删除了第一行后的空白处\\[-2.4ex]


代码

\documentclass[margin=10pt,dvipsnames]{standalone}
\usepackage{tabularx}
\usepackage{pgfplots}

\newcommand{\cbox}[1]{{\color{#1}\vrule height6pt width6pt depth0pt}\kern3pt}

\newcolumntype{Y}{>{\centering}X}
\newcolumntype{Z}{X<{\centering}}
\newcolumntype{C}{>{\centering}m{0.86cm}}

\colorlet{color1}{blue!40}
\colorlet{color2}{red!50}
\colorlet{color3}{LimeGreen!50}
\colorlet{color4}{violet!50}

\pgfplotsset{colored/.style={fill=#1,draw=#1}}

\begin{document}

\begin{tabularx}{12cm}{|C|Y|Y|Z|}
\multicolumn{4}{c}{%
\begin{tikzpicture}
\begin{axis}[
width=12cm,
height=8cm,
yticklabel style={
ymin=0.860,ymax=1.000,
/pgf/number format/precision=3,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,},
axis x line*=bottom,
axis y line*=left,
enlarge x limits=0.3,
xtick=\empty,
symbolic x coords={L,F,LF},
ybar,
ymajorgrids,
bar width=15pt,
ybar=0pt,
]
\addplot[colored=color1]
coordinates {(L,0.952) (F,0.932)
(LF,0.972) };
\addplot[colored=color2]
coordinates {(L,0.969) (F,0.987)
(LF,0.988)};
\addplot[colored=color3]
coordinates {(L,0.969) (F,0.997)
(LF,0.997) };

\addplot[colored=color4]
coordinates {(L,0.917) (F,0.950)
(LF,0.976)};
\end{axis}
\end{tikzpicture}
}\\[-2.4ex]
\cline{2-4}
\multicolumn{1}{C|}{}  &  L  &  F  &LF\\
\hline
\cbox{color1}400       &0.952&0.932&0.972\\
\hline
\cbox{color2}500       &0.969&0.987&0.988\\
\hline
\cbox{color3}600       &0.969&0.997&0.997\\
\hline
\cbox{color4}700       &0.917&0.950&0.976\\
\hline
\end{tabularx}

\end{document}

结果

在此处输入图片描述

答案2

matrix图书馆的另一种选择

代码

\documentclass[margin=10pt,dvipsnames]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}

\newcommand{\cbox}[1]{{\color{#1}\vrule height6pt width6pt depth0pt}\kern3pt}

\colorlet{color1}{blue!40}
\colorlet{color2}{red!50}
\colorlet{color3}{LimeGreen!50}
\colorlet{color4}{violet!50}

\pgfplotsset{colored/.style={fill=#1,draw=#1}}
\tikzset{mynode/.style={draw,minimum width=#1,minimum height=5mm,line width=.5pt}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=12cm,
height=7cm,
yticklabel style={
ymin=0.860,ymax=1.000,
/pgf/number format/precision=3,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,},
axis x line*=bottom,
axis y line*=left,
enlarge x limits={rel=0.25},
xtick=data,
ytick={0.860,0.880,0.900,0.920,0.940,0.960,0.980,1.000},
xtick style={draw=none},
xticklabel style={yshift=2pt},
symbolic x coords={L,F,LF},
ybar,
ymajorgrids,
bar width=15pt,
ybar=0pt,
]
\addplot[colored=color1]
coordinates {(L,0.952) (F,0.932)
(LF,0.972) };
\addplot[colored=color2]
coordinates {(L,0.969) (F,0.987)
(LF,0.988)};
\addplot[colored=color3]
coordinates {(L,0.969) (F,0.997)
(LF,0.997) };
\addplot[colored=color4]
coordinates {(L,0.917) (F,0.950)
(LF,0.976)};
\coordinate (B) at (yticklabel cs:0);
\end{axis}
\node [matrix of nodes,inner sep=-\pgflinewidth,anchor=north west,row sep=-\pgflinewidth,column sep=-\pgflinewidth,nodes in empty cells,
column 1/.style={nodes={mynode=1.04cm}},
column 2/.style={nodes={mynode=3.5cm}},
column 3/.style={nodes={mynode=3.3cm}},
column 4/.style={nodes={mynode=3.6cm}},
] (my matrix) at (B)
{
\node[draw=none]{};&  {} &  {} & {}  \\
%
\cbox{color1}400   &0.952&0.932&0.972\\
%
\cbox{color2}500   &0.952&0.932&0.972\\
%
\cbox{color3}600   &0.952&0.932&0.972\\
%
\cbox{color4}700   &0.952&0.932&0.972\\
};

\end{tikzpicture}

\end{document}

结果

在此处输入图片描述

相关内容