![如何在表格内绘制堆积条形图?](https://linux22.com/image/289916/%E5%A6%82%E4%BD%95%E5%9C%A8%E8%A1%A8%E6%A0%BC%E5%86%85%E7%BB%98%E5%88%B6%E5%A0%86%E7%A7%AF%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%9F.png)
我在 Latex 中有以下代码。它仅绘制一个数据系列 (data2)。我想在表格内为 data1 和 data2 绘制不同颜色的条形图。
有任何想法吗。?
非常感谢。
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
% The data
\begin{filecontents}{data.txt}
accession,data1,data2
C1,150708.4564,481.0388003
C2,49652.53112,275.2190163
C3,37379.1284,58.85806921
C4,34165.85698,26.06571636
C5,30856.19158,35.96802428
C6,15204.22768,26.71594573
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.txt}\data
\newcommand{\errplot}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
ytick=\empty,
axis x line*=bottom,
xbar,
bar width=1.5ex,
xmin=0,
visualization depends on=x \as \rawx,
nodes near coords,
every node near coord/.style={
anchor=east,
shift={(axis direction cs:-\rawx,0)}
}
]
%
\addplot [draw=black, fill=red]
table [x=data2,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval
% Print the table
\pgfplotstabletypeset[columns={accession,data1,data2},
% Booktabs rules
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[3ex]\bottomrule},
% Set header name
columns/accession/.style={string type,column type=l,column name=accession},
columns/data1/.style={string type,column type=l,column name=data1},
columns/data2/.style={
column name={Data2},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=0
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplot}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
}
},
]{\data}
\end{document}
答案1
\newcommand
为每个data
说法定义\newcommand{\errplota}{}
并\newcommand{\errplotb}{}
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
% The data
\begin{filecontents}{data.txt}
accession,data1,data2
C1,150708.4564,481.0388003
C2,49652.53112,275.2190163
C3,37379.1284,58.85806921
C4,34165.85698,26.06571636
C5,30856.19158,35.96802428
C6,15204.22768,26.71594573
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.txt}\data
\newcommand{\errplota}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
ytick=\empty,
axis x line*=bottom,
xbar,
bar width=1.5ex,
xmin=0,
visualization depends on=x \as \rawx,
nodes near coords,
every node near coord/.style={
anchor=east,
shift={(axis direction cs:-\rawx,0)}
}
]
%
\addplot [draw=black, fill=red]
table [x=data1,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}
\newcommand{\errplotb}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
scale only axis,
width=5cm,
enlarge y limits={abs=0.5},
axis y line*=middle,
ytick=\empty,
axis x line*=bottom,
xbar,
bar width=1.5ex,
xmin=0,
visualization depends on=x \as \rawx,
nodes near coords,
every node near coord/.style={
anchor=east,
shift={(axis direction cs:-\rawx,0)}
}
]
%
\addplot [draw=black, fill=yellow]
table [x=data2,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}
\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval
% Print the table
\pgfplotstabletypeset[columns={accession,data1,data2},
% Booktabs rules
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[3ex]\bottomrule},
% Set header name
columns/accession/.style={string type,column type=l,column name=accession},
columns/data1/.style={
column name={Data1},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=0
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplota}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
},},
columns/data2/.style={
column name={Data2},
assign cell content/.code={% use \multirow for Z column:
\ifnum\pgfplotstablerow=0
\pgfkeyssetvalue{/pgfplots/table/@cell content}
{\multirow{\numberofrows}{6.5cm}{\errplotb}}%
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
},
}
]{\data}
\end{document}