表格中的条形图

表格中的条形图

问题是6 年前已解决以一种非常好的方式。但是,有没有办法将该表分成几个部分并更改条形图的颜色 - 就像图片上那样?下面是该响应的代码。

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}

% The data file
% In a real application, this would be a text file in your file system
\begin{filecontents}{data.txt}
function,cpu time
Loop at line 151,3.42
Loop at line 107,3.27
Scalar face value,3.09
Loop at line 102,1.7
Get sensible enthalpy,1.25
Compare vec3,1.14
\end{filecontents}


\pgfplotstableread[col sep=comma]{data.txt}\data

% Define the command for the plot
\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=cpu time,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={function, cpu time},
  % Booktabs rules
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=[3ex]\bottomrule},
  % Set header name
  columns/function/.style={string type,column type=l,column name=Function},
  columns/cpu time/.style={
    column name={CPU Time},
    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}
%Done!
\end{document}

在此处输入图片描述

相关内容