latex-tikz简单绘图

基础环境

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
	\begin{tikzpicture}
		\draw (0,0) -- (1,1);
	\end{tikzpicture}
\end{document}

#命令环境

单行

\tikz \draw (0,0) -- (1,1); % 每次绘图,以;结尾

子域

\tikz {\draw (0,0) -- (1,1); \draw (0,1) -- (1,0);}

环境

\begin{tikzpicture}
    \draw (0,0) -- (1,1);
    \draw (0,1) -- (1,0);
\end{tikzpicture}

后续统一以此中方式进行说明

\begin{tikzpicture}[domain=-3:3] % 规划画布大小
\end{tikzpicture}

包围

\tikzpicture
	\draw (0,0) -- (1,1);
	\draw (1,0) -- (0,1);
\endtikzpicture

坐标

标准

\begin{tikzpicture}
    \drarw (0,0) -- (1,1); % 默认的画图单位为cm
\end{tikzpicture}

单位

\begin{tikzpicture}
	\draw (0pt, 0pt) -- (1pt, 1pt); % 图像显示可以明确的感受到尺度差异
\end{tikzpicture}

相对

\begin{tikzpicture}
	\draw (0,0) -- +(1,1); % (0,0) -- (0+1,0+1)
	\draw (1,2) -- +(1,1); % (1,2) -- (1+1,2+1)
\end{tikzpicture} % 可以使用相对坐标进行偏移

记录

\begin{tikzpicture}
	\draw (0,0) -- +(1,1) -- +(0,1); % (0,0) -- (0+1,0+1) -- (0+0,0+1);
	% 偏移总是以上一次的记录值为准
	\draw (0,0) -- ++(1,1) -- +(0,1); %(0,0) -- (0+1,0+1) -- (0+1+0, 0+1+1)
	% 使用++,表示偏移,且更新上一次记录值
\end{tikzpicture}

极坐标

\begin{tikzpicture}
	\draw (0:1) -- (90:1) -- (180:1) -- (270:1) -- (360:1);
	% (angle, length); 天然支持极坐标
\end{tikzpicture}
\begin{tikzpicture}
	% 两种坐标天然混杂,自动区分
	\draw (0,0) -- (0:1) -- (1,1) -- (90:1) -- (0,0);
\end{tikzpicture}

计算

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc} % 引入计算支持
\begin{document}
	\begin{tikzpicture}
		\draw (1,3) -- ($3*(1,3) - (-2, 5)$); % (1,3) -- (5, 4)
		% 1. 计算内容使用$$包含
		% 2. 进行坐标整体计算,而不是单个部分
	\end{tikzpicture}
\end{document}
\begin{tikzpicture}
\draw (0:1) -- ($(90:100) - (0, 99)$); % 极坐标同样支持计算
\end{tikzpicture}

箭头

\begin{tikzpicture}
	\draw [->]    (0,0) -- (3,0);
	\draw [<-]    (0,1) -- (3,1);
	\draw [<->]   (0,2) -- (3,2);
	\draw [|->]   (0,3) -- (3,3);
	\draw [>->>]  (0,4) -- (3,4);
	\draw [|<->|] (0,5) -- (3,5);
\end{tikzpicture}

图形

三角

\begin{tikzpicture}
	\draw (0,0) -- (0,3) -- (4,0) -- cycle; % cycle 用来填充缺口
\end{tikzpicture}
\begin{tikzpicture}[line width=3pt]
	\draw (0,0) -- (0,3) -- (4,0) -- (0,0); % 有缺口
	\draw (5,5) -- (5,8) -- (9,5) -- cycle; % cycle 用来填充缺口
\end{tikzpicture}

矩形

\begin{tikzpicture}
	\draw (0,0) rectangle (3,3); % 两个点围成的矩形
\end{tikzpicture}

网格

\begin{tikzpicture}
	\draw [step=1] (0,0) grid (5,5); % 从两点规划矩形,然后按照步长进行分割 
\end{tikzpicture}

\begin{tikzpicture}
	\draw (3,3) circle (2); % 中心坐标(3,3), 半径为2,外接矩形为 2x2
	\draw (3,3) ellipse (3 and 2); % 椭圆,后参数分为(x, y)轴长
\end{tikzpicture}

\begin{tikzpicture}
	\draw (3,3) arc (0:180:2); % 上图圆上半部分
	\draw (3,3) arc (180:360:3 and 2); % 上图椭圆下半部分
	% 图形图像不变,但是原来的坐标变为了弧线的起始点
	% 角度按照逆时间方向进行计算
\end{tikzpicture}

函数

抛物线

\begin{tikzpicture}
	\draw (1,1) parabola (3,3); % 默认以起始点为极值点
	\draw (2,1) parabola[bend at end] (4,3); % bend at end: 终点为极值点
	\draw (0,0) parabola bend (3,5) (5,4); % bend:中指定极值点
\end{tikzpicture}

三角

\begin{tikzpicture}
	\draw (0,0) sin (1,1); % sin 右极值
	\draw (1,0) cos (2,1); % cos 左极值
	\draw (2,0) cos (4,2) sin (6,0); % 为了衔接极值点,极值点总是左边sin右边cos
	% 同样的,极值点的转换部分,总是左边cos右边sin,否则曲线不够光滑
	% 因为不能两端都是极值点
\end{tikzpicture}

贝塞尔曲线

\begin{tikzpicture}
	\draw (0,0) .. controls (1,1) .. (4,0); % draw start .. controls ctrl .. end;
	\fill (1,1) circle (1pt); % fill 填充图像
\end{tikzpicture}

二次贝塞尔曲线,首尾两端切线都过指定点

\begin{tikzpicture}
	\draw (0,0) .. controls (1,1) and (3,-1) .. (4,0); % ctrl: A and B
	\fill (1,1) circle (1pt);
	\fill (3,-1) circle (1pt);
\end{tikzpicture}

三次就是两个控制点

连线

\begin{tikzpicture}
	\draw (0,0) to (4,0); % 同 --, 默认情况下直线相连
	\draw (0,0) to[out=60,in=-120] (4,0);
	% out: 出线角度
	% in : 收线角度
\end{tikzpicture}

函数

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
	\begin{tikzpicture}[domain=-3:3]
	\draw [->] (-3.5,0) -- (3,0) node[below] {$x$}; 			% x轴
	\draw [->] (0,-3.5) -- (0, 3.5) node[above] {$f(x)$};	% y轴
	\fill (0,0) circle (3pt);															% 原点
	\draw [step=1,color=gray] (-3,-3) grid (3,3); 				% 网格
	\draw [color=orange] plot (\x, \x); 									% y = x
	\draw [color=red] plot (\x, {\x^2 / 3}); 							% y = x ^ 2 / 3
	\draw [color=cyan] plot (\x, {3 * sin(\x r)}); 				% y = 3 sin(x)
	\end{tikzpicture}
\end{document}

node


node[direction] symbol; % 后续进行说明

plot

\draw [color=color] plot (x, y); % 其中\x表示符号
  • 如果是复杂计算式,包含括号,则需要使用{}进行包含
  • 三角运算,以角度计算,应该转化为弧度\x r

坐标系

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newenvironment{axis}[1]
{	% 坐标系环境定义
	\begin{tikzpicture}[domain=-#1:#1]
	\draw ($(-#1,0)-(0.5,0)$) -- ($(#1,0)+(0.5,0)$) node[below] {$x$};
	\draw ($(0,-#1)-(0,0.5)$) -- ($(0,#1)+(0,0.5)$) node[above] {$f(x)$};
	\fill (0,0) circle (1pt);
	\draw [step=1, color=gray] (-#1,-#1) grid (#1,#1);
}
{\end{tikzpicture}}
\begin{document}
	\begin{axis}{3}
		\draw [color=red] plot (\x, {sin(\x r)})
	\end{axis}
\end{document}

填充

\begin{tikzpicture}
	% 画图,仅仅描绘边框
	\draw [blue] (0,0) rectangle (1,1);
	\draw [green] (3,3) circle (1);
	% 填充,仅仅填充颜色
	\fill [green] (0,0) rectangle (1,1);
	\fill [blue] (3,3) circle (1);
	% 双管齐下
	\filldraw[fill=yellow, draw=red] (2,2) circle (1);
\end{tikzpicture}

渐变

\begin{tikzpicture}
	\shade [inner color=yellow, outer color=orange] (2,2) circle (1);
	% inner 
	% outer
	\shade [left color=blue,right color=green] (3,3) rectangle (1,1);
	% left
	% right
	% top
	% bottom
	% 可填充着六个维度,一般选择一对即可,颜色太多并没有想要的效果
\end{tikzpicture}

node

偏移

\begin{tikzpicture}
\fill (0,0) circle (1pt);
\node[above] at (0,0) {$A$}; % \node[direction] at (location) {$label$};
\node[below] at (0,0) {$B$}; % 上下左右=>(above, below, left, right)
\node[left]  at (0,0) {$L$};
\node[right] at (0,0) {$R$};
\end{tikzpicture}

名称

\begin{tikzpicture}
  % 指定点放置标签
	\node (a) at (2,1)  {$A$};
  \node (b) at (2,-1) {$B$};
  \node (l) at (1,0)  {$L$};
  \node (r) at (3,0)  {$R$};
  % 标签可以按照点一样连线
  \draw[->,color=orange] (a) -- (l);
  \draw[->,color=green]  (l) -- (b);
  \draw[->,color=blue]   (b) -- (r);
  \draw[->,color=cyan]   (r) -- (a);
\end{tikzpicture}

形状

\begin{tikzpicture}
	\node [shape=circle] (a) at (0,0) {$C$};     	% circle
	\node [shape=rectangle] (b) at (1,1) {$R$};		% rectangle, 默认为矩形
	\node [shape=coordinate] (c) at (2,2) {$P$};	% coordinate
	\draw[->] (a) -- (b) -- (c);	% coordinate无区域,会覆盖任何标签
\end{tikzpicture}

填充

\begin{tikzpicture}
	\node [fill=gray] at (0,0) {$F$};
\end{tikzpicture}

相对

% \usetikzlibrary{position}
\begin{tikzpicture}
\node [fill=orange] (a) at (0,0) {$A$};
\node [fill=red, right=4 of a ] (b) {$B$}; % 根据已有的node,直接通过相对位置指定和位移即可定位
\node [fill=blue, above left=2 of b] (c) {$C$};
\draw (a) -- (b) -- (c) -- (a);
\end{tikzpicture}

\usetikzlibrary{position}千万不能忘记!

融合

\begin{tikzpicture}
	\draw (0,0) node[above right] {$A$} -- (3,0) node[right] {$B$} -- (0,4) node[above]{$C$} -- cycle;
	% 点后面直接可以跟node
	% 同样可以设置属性和标签
	% 相对位置为点的坐标
\end{tikzpicture}

路径

线段

\begin{tikzpicture}
	\path[draw] (0,0) -- (1,1); % draw: 画路径
\end{tikzpicture}

图形

\begin{tikzpicture}
	\path[draw=cyan, fill=yellow] (0,0) -- (3,0) -- (0,3) -- cycle; 
	% draw: 连线
	% fill: 填充
\end{tikzpicture}

其他

\begin{tikzpicture}
% draw, shade
\path[draw=black, inner color=yellow, outer color=orange] (3,3) circle (1);
% fill
\path[fill=blue] (2,2) rectangle (1,1);
% shade
\path[left color=red, right color=green] (0,0) .. controls (1,1) and (3,3) .. (6,0);
\path[fill](3,3) circle(1pt);
\path[fill](2,2) circle(1pt);
% node
\path[fill=gray](3,3) node[below] (a) {$P$};
\end{tikzpicture}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值