答案1
我猜这是一种条形图。通常我会使用pgfplots
它,但由于我不知道如何执行您的要求,我展示了一个选项,即使用纯 TikZ 手动构建图。因此,它可能不如方法灵活pgfplots
。重叠是通过绘制两个条形图并以 50% 的不透明度来处理的。
\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplotstable}
\pgfplotstableread{ % save data to table
Mmin Mmax Wmin Wmax label
20 60 30 80 All
10 50 30 70 completed
20 90 60 100 dropped
}\datatable
% get number of rows
\pgfplotstablegetrowsof{\datatable}
% subtract 1 because table indices start at 0
\pgfmathsetmacro{\Nrows}{\pgfplotsretval-1}
\begin{document}
\begin{tikzpicture}[
bars/.style={draw,line width=5mm,opacity=0.5},
women/.style={bars,color=red},
men/.style={bars,color=blue},
axis/.style={-stealth,thick},
y=3cm
]
\foreach [count=\X from 1] \i in {0,...,\Nrows} {
% get values from table
\pgfplotstablegetelem{\i}{Mmin}\of\datatable \pgfmathsetmacro{\Mmin}{\pgfplotsretval/100}
\pgfplotstablegetelem{\i}{Mmax}\of\datatable \pgfmathsetmacro{\Mmax}{\pgfplotsretval/100}
\pgfplotstablegetelem{\i}{Wmin}\of\datatable \pgfmathsetmacro{\Wmin}{\pgfplotsretval/100}
\pgfplotstablegetelem{\i}{Wmax}\of\datatable \pgfmathsetmacro{\Wmax}{\pgfplotsretval/100}
% draw bars
\path [men] (\X,\Mmin) -- (\X,\Mmax);
\path [women] (\X,\Wmin) -- (\X,\Wmax);
% add labels below x-axis
\pgfplotstablegetelem{\i}{label}\of\datatable
\node [rotate=40,below,anchor=north east,font=\footnotesize] at (\X,0) {\pgfplotsretval};
}
% draw axes
\draw [axis] (0,-0.1cm) -- (0,1.1);
\draw [axis] (-0.1cm,0) -- (\Nrows+2,0);
% make ticks on y-axis
\foreach [evaluate=\y as \Y using int(round(\y*100)))] \y in {0.2,0.4,...,1}
\draw (2pt,\y) -- (-2pt,\y) node[left] {\Y \%};
% legend
\path [women] (\Nrows+2,0.7) -- ++(3mm,0) node[right,opacity=1,black] {Women};
\path [men] (\Nrows+2,0.5) -- ++(3mm,0) node[right,opacity=1,black] {Men};
\end{tikzpicture}
\end{document}