如何在 Tikz 中绘制具有笛卡尔坐标轴和尺寸约束的矩形

如何在 Tikz 中绘制具有笛卡尔坐标轴和尺寸约束的矩形

我想使用 lateX 中的 Tikz-PGF 包绘制与附件完全相同的图形。我该怎么做?我很感激任何帮助,因为我必须提交一篇论文,这是最后一刻才添加的。谢谢在此处输入图片描述

答案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}

相关内容