我正在尝试使用 Tikz 编辑 2D 图形。我当前的代码和图如下:
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}`
\begin{document}
\begin{figure}[hbt]
\centering
\begin{tikzpicture}[scale=350]
\centering
\draw[blue,thick] (0,0) -- (0.0219837,0)-- (0.0219837,0.0168148)-- (0.0056579,0.0168148) -- (0.0052959,0.0127) -- (0.0027051,0.0127) -- (0.0027051,0.0142494)--(0,0.0142494) -- cycle;
%\filldraw[fill=green!20,draw=green!50!black]
\draw[line width=1mm, red] (0.0024, 0.0142495) -- (0.0024, 0.01225);
\draw[line width=1mm, green] (0.0024, 0.0122495) -- (0.0024, 0.002);
\draw[line width=1mm, black] (0.0024, 0.002, 0.0) -- (0.0024, 0.0);
\draw[line width=0.55mm, magenta] (0.0, 4.0E-8) -- (0.0219837, 4.0E-8);
\end{tikzpicture}
\caption{2D domain.}
\label{fig:2D Domain}
\end{figure}
\end {document}
这为我提供了下图:
现在,我想按如下方式编辑我的图形:即每条新添加的线(3 条垂直线、1 条水平线)都应具有图中所示的相应颜色的图例。例如,我使用了红色、绿色、蓝色和洋红色。另外,我希望它们对齐。
答案1
只需在所需位置绘制线条并在其末端添加一个节点即可。例如:
\draw[line width=1mm, red] (0.005,0.01) -- (0.0075,0.01) node [right] {Line 1};
我不知道您是否需要保留线宽,如果不需要,请将其删除。
这可能是完整的代码:
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=350]
\centering
\draw[black,thick] (0,0) -- (0.0219837,0) -- (0.0219837,0.0168148) -- (0.0056579,0.0168148) --
(0.0052959,0.0127) -- (0.0027051,0.0127) -- (0.0027051,0.0142494) -- (0,0.0142494) -- cycle;
\draw[line width=1mm, red] (0.0024, 0.0142495) -- (0.0024, 0.01225);
\draw[line width=1mm, green] (0.0024, 0.0122495) -- (0.0024, 0.002);
\draw[line width=1mm, blue] (0.0024, 0.002) -- (0.0024, 0.0);
\draw[line width=0.55mm, magenta] (0.0, 4.0E-8) -- (0.0219837, 4.0E-8);
% Legend
\draw[line width=1mm, red] (0.005,0.01) -- (0.0075,0.01) node [right] {Line 1};
\draw[line width=1mm, green] (0.005,0.0085) -- (0.0075,0.0085) node [right] {Line 2};
\draw[line width=1mm, blue] (0.005,0.007) -- (0.0075,0.007) node [right] {Line 3};
\draw[line width=0.55mm, magenta] (0.005,0.0055) -- (0.0075,0.0055) node [right] {Line 4};
\end{tikzpicture}
\end{document}