我想在表格中添加刻度(xaxis)。以下是来自 DRi 的代码(如何在表格中的条形图中添加比例)。
我确实发现轴标签向右移动了。我可以将它们放在刻度线所在的中心位置吗?
或者还有其他方法可以将比例放在底部吗?
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\mybar#1{%%
#1s & {\color{red}\rule{#1cm}{8pt}}}
\pagestyle{empty}
\begin{document}
\begin{tabular}{>{$\rhd$ }lrl}
Loop at line 151 in divergence & \mybar{3.420}\\
Loop at line 1071 in radiation & \mybar{3.270}\\
using tikzscalar face value & \mybar{3.090}\\
Loop at line 102 in get & \mybar{1.700}\\
get sensible enthalpy & \mybar{1.250}\\
&& \begin{tikzpicture}
\draw[->] (0,0) -- (4.5,0);
\foreach \x in {0,1,...,4}
\draw[thin] (\x,0) -- (\x,-0.1);
\foreach \x in {0,1,...,4}
\node at ({\x+0.15}, {0-0.1}) [below] {\x};
\end{tikzpicture}\\
\end{tabular}
\end{document}
答案1
只需将最终\node
命令从更改\node at ({\x+0.15},....)...
为\node at ({\x},...)...
并将 添加trim left
到此处即可tikzpicture
。
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\mybar#1{%%
#1s & {\color{red}\rule{#1cm}{8pt}}}
\pagestyle{empty}
\begin{document}
\begin{tabular}{>{$\rhd$ }lrl}
Loop at line 151 in divergence &\mybar{3.420}\\
Loop at line 1071 in radiation &\mybar{3.270}\\
using tikzscalar face value &\mybar{3.090}\\
Loop at line 102 in get &\mybar{1.700}\\
get sensible enthalpy &\mybar{1.250}\\
\multicolumn{1}{c}{}&&\begin{tikzpicture}[trim left]
\draw[->] (0,0) -- (4.5,0);
\foreach \x in {0,1,...,4}
\draw[thin] (\x,0) -- (\x,-0.1);
\foreach \x in {0,1,...,4}
\node at ({\x}, {0-0.1}) [below] {\x};
\end{tikzpicture}\\
\end{tabular}
\end{document}
如果没有trim left
,则刻度的最左边的点是0
字符的左边,所以这个图像会向右移动。 trim left
确保图片的最左边对应于x=0
图片中的坐标。
正如 Zarko 所指出的,我还添加了\multicolumn
代码来删除最后一行上的虚假三角形标记。