![如何在 Tikz 中绘制具有笛卡尔坐标轴和尺寸约束的矩形](https://linux22.com/image/329924/%E5%A6%82%E4%BD%95%E5%9C%A8%20Tikz%20%E4%B8%AD%E7%BB%98%E5%88%B6%E5%85%B7%E6%9C%89%E7%AC%9B%E5%8D%A1%E5%B0%94%E5%9D%90%E6%A0%87%E8%BD%B4%E5%92%8C%E5%B0%BA%E5%AF%B8%E7%BA%A6%E6%9D%9F%E7%9A%84%E7%9F%A9%E5%BD%A2.png)
答案1
这项服务“为我而做”......
\begin{tikzpicture}
\draw[<->] (-1,0.0) -- (6,0) node[below left] {$x$};
\draw[<->] (0,-0.5) -- (0,3) node[below left] {$y$};
\draw[very thick] (0,0) |- (5,1) -- (5,0)
(2,0) |- (3,2) -- (3,0);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(2,-0.4) -- node[fill=white] {$W$} (3,-0.4);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(0,-0.8) -- node[fill=white] {$GW$} (5,-0.8);
\end{tikzpicture}
要使上述代码可行,您需要在前言中添加\usetikzlibrary{arrows.meta}
。结果:
附录: 正如下面评论中所要求的那样......以下是“矩形”可能着色的三种情况:
生成上述图像的完整小文档称为最小工作示例 (MWE),
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
% colored lines
\begin{tikzpicture}
\draw[<->] (-1,0.0) -- (6,0) node[below left] {$x$};
\draw[<->] (0,-0.5) -- (0,3) node[below left] {$y$};
\draw[red,very thick] % <--- changed
(0,0) |- (5,1) -- (5,0);
\draw[blue,very thick] % <--- added
(2,0) |- (3,2) -- (3,0);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(2,-0.4) -- node[fill=white] {$W$} (3,-0.4);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(0,-0.8) -- node[fill=white] {$GW$} (5,-0.8);
\end{tikzpicture}
\begin{tikzpicture}
% colored lines and fill
\draw[<->] (-1,0.0) -- (6,0) node[below left] {$x$};
\draw[<->] (0,-0.5) -- (0,3) node[below left] {$y$};
\draw[draw=red,fill=red!30,very thick] % <--- changed
(0,0) |- (5,1) -- (5,0);
\draw[draw=blue,fill=blue!30,very thick] % <--- added
(2,0) |- (3,2) -- (3,0);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(2,-0.4) -- node[fill=white] {$W$} (3,-0.4);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(0,-0.8) -- node[fill=white] {$GW$} (5,-0.8);
\end{tikzpicture}
\begin{tikzpicture}
% colored lines and fill, added transparency
\draw[<->] (-1,0.0) -- (6,0) node[below left] {$x$};
\draw[<->] (0,-0.5) -- (0,3) node[below left] {$y$};
\draw[draw=red,fill=red!30,very thick,semitransparent] % <--- changed
(0,0) |- (5,1) -- (5,0);
\draw[draw=blue,fill=blue!30,very thick,semitransparent]% <--- changed
(2,0) |- (3,2) -- (3,0);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(2,-0.4) -- node[fill=white] {$W$} (3,-0.4);
\draw[{Bar[width=4mm].Straight Barb[]}-{Straight Barb[].Bar[width=4mm]}]
(0,-0.8) -- node[fill=white] {$GW$} (5,-0.8);
\end{tikzpicture}
\end{document}