答案1
一个简单的方法是使用 tikz 包(texdoc tikz
在终端中输入手册),正如 daleif 所建议的那样。我认为下面的代码非常容易理解,并且易于调整(例如,如果矩形需要额外的 2mm)。最后绘制的项目在\foreach
循环内以避免重复代码,但我希望它足够清晰。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
% Colors
\definecolor{mygreen}{HTML}{67FF66}
\definecolor{mycyan} {HTML}{BDE1E4}
\definecolor{mypink} {HTML}{FF66FF}
\begin{document}
\begin{tikzpicture}[line join=round]
% Axis
\draw[thick,-latex] (0,0) -- (6,0);
% Dashed line
\draw[dashed] (5,0) -- (5,1.5);
% Red dots
\draw[fill=red] (0,0) circle (1 mm) node [yshift=-3 mm] {$0$};
\draw[fill=red] (5,0) circle (1 mm) node [yshift=-3 mm] {$t$};
% Rectangles
\draw[fill=mypink] (0,0.5) rectangle (4 ,1);
\draw[fill=mygreen] (0,1) rectangle (3.2,1.5);
\draw[fill=yellow] (0,1.5) rectangle (5 ,2);
\draw[fill=mycyan] (0,2) rectangle (4.2,2.5);
% Division lines
\foreach\x/\y in {1/0.5, 2/0.5, 3/0.5, 1.7/1, 1.3/1.5, 2.7/1.5, 1/2}
{%
\draw (\x,\y) --++ (0,0.5); % <-- relative coordinate, 0.5 cm above (\x,\y) point
}
% Labels
\foreach\i in {1,...,4}
{%
\node at (-0.5,2.75-0.5*\i) {$M_\i$};
}
\end{tikzpicture}
\end{document}