Advertisement

Latex 绘图:Tikz 包

阅读量:

参考文献:

  1. The TikZ and PGF Packages
  2. TiKZ入门教程 - LaTeX工作室 (latexstudio.net)
  3. Latex-TiKZ绘制数学平面几何图教程_latex绘制几何图形-博客
  4. 【TikZ 简单学习(上):基础绘制】Latex下的绘图宏包-博客
  5. LaTeX中TikZ绘图备忘-博客
  6. LaTeX—Tikz 宏包入门使用教程 - 知乎 (zhihu.com)
  7. Latex 实时编译 & TikZ 绘图
  8. Pattern Library
  9. 分享 | 顶刊高质量论文插图配色(含RGB值及16进制HEX码)

文章目录

  • 基本语法
  • 高级功能
    • 树形图
    • 关系图
    • 填充样式
    • 双轴 & 子图

基本语法

首先 \usepackage{tikz} 导入包,基本的指令是:

复制代码
    \begin{center}
    	\begin{tikzpicture}
    		\coordinate(a1) at (0,0); 		%将坐标 (0,0) 标记为 a1
    		\coordinate(a2) at (5,0);
    		\coordinate(a3) at (0,5);
    		\coordinate(a4) at (1,-1);
    		
    		\coordinate(b1) at (1,1);
    		\coordinate(b2) at (2,1);
    		\coordinate(b3) at (2,2);
    		\coordinate(b4) at (1,2);
    		
    		\coordinate(c1) at (1,3);
    		\coordinate(c2) at (4,4);
    		\coordinate(c3) at (3,-3);
    		
    		\node at (a1)[left]{$a_1$};		%在位置 a1 书写标识
    		\node at (a2)[above]{$a_2$};
    		\node at (a3)[right]{$a_3$};
    		\node at (a4)[below]{$a_4$};
    		
    		\node at (b2)[shift={(0.6,-0.2)},rotate=45]{let $b=\hat\alpha_\kappa \cdot \frac{1}{\beta}$};	%偏移+旋转
    		
    		\fill (a1) circle (0.5mm); 		%在位置 a1 绘制圆圈
    		\fill (a2) circle (0.5mm);
    		\fill (a3) circle (0.5mm);
    		\fill (a4) circle (0.5mm);
    		
    		\fill (c1) circle (1mm);
    		\fill (c2) circle (1mm);
    		\fill (c3) circle (1mm);
    		
    		\draw[line width=0.3mm,color=blue] (a2) -- (a3);	%绘制从 a1 到 a2 的线段
    		\draw[dash dot,->] (a1) -- (a2);					%箭头
    		\draw[loosely dash dot,<->] (a1) -- (a3);			%双箭头
    		\draw[] (b1)--(b2)--(b3)--(b4)--cycle;				%闭合线段
    		
    		\draw (a2) circle (1cm);						%圆圈
    		\draw (a2) ellipse (1cm and 2cm);				%椭圆
    		\draw[color=red] (a1) rectangle (a4);			%矩形
    		
    		\draw (a3) .. controls (c1) and (c2) .. (a2);	%由 c1,c2 控制的贝塞尔曲线
    		\draw (a1) parabola bend (c3) (a2);				%以 c3 为顶点的抛物线
    		
    		\draw[step=1,color=gray!40] (-2,0) grid (0,4);				%绘制网格
    		\draw[latex-latex, red] (0,2) -- ++(-1,1) -- ++(-1,-1);		%更新的偏移
    		\draw[dashed, blue] (0,1) -- +(-1,1) -- +(-2,0);			%不更新的偏移
    		
    		\draw[domain=0:4] plot (\x,{0.1*exp(\x)}) node[right]{$f(x)=\frac{1}{10}e^x$};	%绘制函数图像
    	\end{tikzpicture}
    \end{center}
    
    
    latex
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/aIKz3wMknBl6tv08JqWgciE7LDHr.png)

绘制结果:
在这里插入图片描述

高级功能

树形图

复制代码
    \begin{center}
    	\tikzset{
    		box/.style ={
    			rectangle, 				%矩形节点
    			rounded corners=5pt, 	%圆角
    			minimum width=50pt, 	%最小宽度
    			minimum height=20pt, 	%最小高度
    			inner sep=5pt, 			%文字和边框的距离
    			draw=blue 				%边框颜色
    		}
    	}
    	
    	\begin{tikzpicture}[sibling distance = 80pt]	%树形图
    		\node[box] {root}
    		child {node[box] {a1}}
    		child {node {a2}
    			child {node {b1}}
    			child {node {b2}}}
    		child {node[box] {a3}};			
    	\end{tikzpicture}
    \end{center}
    
    
    lat
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/kyz3ucS0wlFoWKts9LraADGE6QqU.png)

绘制结果:
在这里插入图片描述

观察到后画的图形会覆盖先画的图形,因此也可以自己写:

复制代码
    \begin{figure}[tp!]
    	\centering
    	\caption{
    		A Tree.
    	}
    	\label{fig1}
    	
    	\tikzset{
    		box/.style ={
    			shape=rectangle, 		%矩形
    			rounded corners=5pt, 	%圆角
    			minimum width=10pt, 	%最小宽度
    			minimum height=10pt, 	%最小高度
    			inner sep=3pt, 			%文字和边框的距离
    			align = center,			%文字居中对齐
    			draw=white,				%边框颜色
    			fill=white				%填充颜色
    		}
    	}
    	
    	\begin{tikzpicture}[sibling distance = 100pt]
    		\coordinate(f) at (0,0);
    		\coordinate(c) at (-4,-1);
    		\coordinate(q) at (0,-1);
    		\coordinate(r) at (4,-1);
    
    		\coordinate(c1) at (-1.2,-2);
    		\coordinate(q1) at (0,-2);
    		\coordinate(r1) at (1.2,-2);
    
    		\coordinate(c2) at (2.8,-2);
    		\coordinate(q2) at (4,-2);
    		\coordinate(r2) at (5.2,-2);
    
    		\draw (c) -- (f) -- (r) -- (r2);
    		\draw (f) -- (q) -- (c1);
    		\draw (q1) -- (q) -- (r1);
    		\draw (c2) -- (r) -- (q2);
    		
    		%先画线,再描点,置于顶层
    		\node[box] at (f)[]{$f(X)$};
    		\node[box] at (c)[]{$c(X)$};
    		\node[box] at (q)[]{$q(X)$};
    		\node[box] at (r)[]{$r(X)$};
    		
    		\node[box] at (c1)[]{$c_1(X)$};
    		\node[box] at (q1)[]{$q_1(X)$};
    		\node[box] at (r1)[]{$r_1(X)$};
    		
    		\node[box] at (c2)[]{$c_2(X)$};
    		\node[box] at (q2)[]{$q_2(X)$};
    		\node[box] at (r2)[]{$r_2(X)$};
    	\end{tikzpicture}
    \end{figure}
    
    
    latex
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/5uV1AaBFG3WYEmHv8X6hljb7gRtI.png)

绘制结果:
在这里插入图片描述

关系图

复制代码
    \begin{center}
    	\usetikzlibrary{graphs}	%关系图
    	\begin{tikzpicture}
    		\graph {
    			"$x_1$" -> "$x_2$"[red] -> "$x_3\|x_4$";
    			"$x_1$" ->[bend left] "$x_3\|x_4$";
    		};
    	\end{tikzpicture}
    	
    	\begin{tikzpicture}
    		\coordinate(a1) at (0,0);
    		\coordinate(a2) at (5,0);
    		\coordinate(a3) at (2,3);
    		
    		\node at (a1)[left]{$a_1$};
    		\node at (a2)[above]{$a_2$};
    		\node at (a3)[right]{$a_3$};
    		
    		\fill (a1) circle (0.5mm);
    		\fill (a2) circle (0.5mm);
    		\fill (a3) circle (0.5mm);
    		
    		\graph {
    			(a1) -> (a2) -> (a3);
    			(a1) ->[thick,blue,bend left,dashed] (a3);
    		};
    	\end{tikzpicture}
    \end{center}
    
    
    lat
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/JWTbz3HL9aldqNeR085scrvgyUpu.png)

绘制结果:
在这里插入图片描述

填充样式

在文档的导言区引入 \usetikzlibrary{patterns},启用 pattern 包。

在绘图时,可以添加 [pattern=xxx] 配置。基本的填充模式有:
在这里插入图片描述
在这里插入图片描述

除此之外,可以自定义填充模式,详见 https://tikz.dev/library-patterns

利用它绘制柱状图:

复制代码
    \begin{figure}[!tb]
    	\centering
    	\begin{tikzpicture}
    		\begin{axis}
    			[ybar,					% 柱状图
    			bar width=0.5cm,		% 柱子宽度
    			title={title},
    			axis y line=left,		% 带箭头的y轴
    			axis x line=bottom,		% 带箭头的x轴
    			xtick distance=1,		% x的刻度距离
    			ytick distance=5,		% y的刻度距离
    			xmin=0.5, xmax=3.5,		% x的范围
    			ymin=1, ymax=48,		% y的范围
    			legend pos=north west,	% 图例的位置
    			xlabel={x},
    			ylabel={y},
    			nodes near coords,		% 自动显示node坐标
    			every node near coord/.append style={font=\scriptsize},
    			ymajorgrids,			% 显示y轴刻度的网格线
    			y grid style={white!69.019608!black},
    			grid style={thick,black,dashed},
    			]
    			\addlegendentry{A1}
    			\addplot+ [pattern=north east lines,pattern color=blue] coordinates {(1,27.7) (2,35.9) (3,41.4)};		% 蓝色斜线
    			\addlegendentry{A2}
    			\addplot+ [pattern=horizontal lines,pattern color=red] coordinates {(1,20.7) (2,21.3) (3,20.0)};		% 红色水平线
    			\addlegendentry{A3}
    			\addplot+ [pattern=north west lines,pattern color=orange] coordinates {(1,14.8) (2,17.2) (3,17.4)};		% 橙色反斜线
    		\end{axis}
    	\end{tikzpicture}
    		\caption{
    		Caption.
    	}
    	\label{fig1}
    \end{figure}
    
    
    lat
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/bjnOLSFDRicsKYTrAfzgHyPqZphG.png)

绘制结果如下:
在这里插入图片描述

双轴 & 子图

想要绘制双轴的柱状图 + 折线图,并且把两个图像并列排版,可以使用 \pgfplotsset{set layers}\begin{minipage}

复制代码
    \begin{figure}[!tb]
    	\begin{minipage}{0.42\linewidth}			% 在子页面上绘制子图
    		\scalebox{0.6}{							% 恰当缩放子图
    			\begin{tikzpicture}
    				\pgfplotsset{set layers}		% 设置多个图层,以绘制双轴图
    				\begin{axis}[ybar,
    					scale only axis,
    					title={T1},
    					axis x line*=bottom,		% 不带箭头的x轴
    					axis y line*=left,			% 不带箭头的y轴
    					xmin=0.5, xmax=3.5,
    					ymin=1, ymax=250,
    					xtick distance=1,
    					ytick distance=50,
    					xlabel={XXX},
    					ylabel={YYY},
    					legend style={font=\footnotesize, 	% 设置图例的属性
    						legend pos=north west},
    					legend columns=2,					% 水平排列的图例
    					nodes near coords,
    					every node near coord/.append style={font=\scriptsize},
    					ymajorgrids,
    					y grid style={white!69.019608!black},
    					grid style={thick,black,dashed},
    					]
    					\addlegendentry{A}
    					% 自定义的 RGB 色彩
    					\addplot+ [color={rgb,255:red,107;green,126;blue,185}] coordinates {(1,55) (2,99) (3,123)};		
    					\addlegendentry{B}
    					% 必须用 rgb,255 打头,才能启用范围 [0,255] 的色素值 
    					\addplot+ [color={rgb,255:red,255;green,127;blue,0}] coordinates {(1,33) (2,44) (3,66)};		
    				\end{axis}
    				\begin{axis}[scale only axis,
    					axis x line=none,			% 第二张图,不画横轴
    					axis y line*=right,			% 在右侧绘制第二个纵轴
    					xmin=0.5, xmax=3.5,
    					ymin=1, ymax=150,
    					xtick distance=64,
    					ytick distance=30,
    					ylabel={\ref{z}ZZZ},		% 引用下面的线条形状
    					nodes near coords,
    					every node near coord/.append style={font=\scriptsize},
    					]
    					\addlegendentry{C}
    					\addplot [color=darkgray,mark=*] coordinates {(1,44.4) (2,88.8) (3,123.4)};\label{z}		% 标记这个线条
    				\end{axis}
    		\end{tikzpicture}}
    	\end{minipage}
    	\qquad\space\space					% 两个子图之间的空格,防止重叠
    	\begin{minipage}{0.42\linewidth}
    		\scalebox{0.75}{
    			\begin{tikzpicture}
    		\begin{axis}
    			[ybar,					% 柱状图
    			bar width=0.5cm,		% 柱子宽度
    			title={title},
    			axis y line=left,		% 带箭头的y轴
    			axis x line=bottom,		% 带箭头的x轴
    			xtick distance=64,		% x的刻度距离
    			ytick distance=5,		% y的刻度距离
    			xmin=0.5, xmax=3.5,		% x的范围
    			ymin=1, ymax=48,		% y的范围
    			legend pos=north west,	% 图例的位置
    			xlabel={x},
    			ylabel={y},
    			nodes near coords,		% 自动显示node坐标
    			every node near coord/.append style={font=\scriptsize},
    			ymajorgrids,			% 显示y轴刻度的网格线
    			y grid style={white!69.019608!black},
    			grid style={thick,black,dashed},
    			]
    			\addlegendentry{A1}
    			\addplot+ [pattern=north east lines,pattern color=blue] coordinates {(1,27.7) (2,35.9) (3,41.4)};		% 蓝色斜线
    			\addlegendentry{A2}
    			\addplot+ [pattern=horizontal lines,pattern color=red] coordinates {(1,20.7) (2,21.3) (3,20.0)};		% 红色水平线
    			\addlegendentry{A3}
    			\addplot+ [pattern=north west lines,pattern color=orange] coordinates {(1,14.8) (2,17.2) (3,17.4)};		% 橙色反斜线
    		\end{axis}
    	\end{tikzpicture}}
    	\end{minipage}
    	\caption{
    		Caption.
    	}
    	\label{fig2}
    \end{figure}
    
    
    lat
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/O0bxlsuXtDW4P19SgmUBj53ZwEnV.png)

绘制的结果如下:
在这里插入图片描述

全部评论 (0)

还没有任何评论哟~