TIKZ——LaTeX基本绘图
  TIKZ是LaTeX的⼀个绘图包,可以绘制其他软件很难画出来的图像。
基本⽤法
直线、垂⾜、矩形、椭圆
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}
\draw[->] (0,0)--(7,0);
\draw[->] (0,0)--(0,7); %箭头线
\draw[red] (2,1) -| (1,2);%直⾓1
\draw[blue] (2,1)|-(1,2);%直⾓2
\draw[green] (2,2) circle (1);%圆:圆⼼、半径
\draw[black] (4,4) ellipse (1 and 3);%椭圆:短、长半轴
\draw[yellow] (3,3) rectangle (4,1);%矩形
\draw[orange] (0,0) -- (2,1-|1,2);%到垂点并与(0,0)连线
\draw[purple] (0,1)--(1,1.5)--(0,2)--cycle  %封闭的线段
(0,2)--(1,3);%不加分号的连写
\end{tikzpicture}
\end{document}
  效果图:
圆弧、椭圆弧、三⾓函数曲线、贝塞尔曲线
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}
\draw[->] (0,0)--(7,0);
\draw[->] (0,0)--(0,7); %箭头线
\draw[red] (3,2) arc (0:120:1);%圆弧:起始点,开始⾓度:结束⾓度:半径
\draw[blue] (3,2) arc (0:120:1 and 2);%椭圆弧
\draw[green] (0,0) sin (3,1) cos (5,0);%画pi/2的正弦、余弦
\draw[orange] (3,3)..controls (4,4)and(5,5) .. (4,3); %贝塞尔曲线:起点..controls 控制点 and 控制点..终点
\end{tikzpicture}
\end{document}
  效果图:
辅助线、⽹格、填充、⾃定义函数
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}
\draw[help lines,step = 0.5] (-3,-3) grid (3,3); %辅助线格⼦
\draw[-latex] (-4,0) -- (4,0);
\draw[-latex] (0,-4) -- (0,4);%实⼼箭头
\draw[domain = -2:360][samples = 200] plot({cos(\x)}, {0.5*sin(\x)});%函数图像,参数⽅程,内有⼩括号,外⾯必须⽤花括号括起来,samples是画函数图时列出的点    \filldraw[fill = yellow,draw = blue][ultra thick] (2,2) circle (1);%填⾊,还可以设置线宽
\end{tikzpicture}
\end{document}
  效果图:
图形旋转、平移、缩放、变形、圆⾓多边形、箭头设置
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}[>=Stealth]%设置箭头,环境中所有箭头都⽤这个库
\draw[->] (-4,0) -- (4,0);
\draw[->>] (0,-4) -- (0,4);
\draw(-3,2)--(-3,3)[rounded corners = 0.3cm]
-- (-2,3)--(-1.5,2)[sharp corners]--(-2.5,1)--cycle;%圆⾓多边形
\draw[help lines](0,0) rectangle (1,1);%辅助线
\draw[scale=1.5] (0,0) rectangle (1,1);%缩放
\draw[rotate=30] (0,0) rectangle (1,1);%扰起点旋转
\draw[help lines](2,0) rectangle (3,1);
\draw[shift={(0.5,0.5)}](2,0) rectangle (3,1);%平移
\draw[help lines](4,0) rectangle (5,1);
\draw[xslant=0.4](4,0) rectangle (5,1);%倾斜
\end{tikzpicture}
\end{document}
  效果图:
锚点、⾃定义属性
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0);
\draw[->>] (0,-4) -- (0,4);
\node (A) at (2,2) {$A_1$};
\node[draw] (B) at (3,2) {B};%draw属性添加⾃适应⼤⼩的⽅框
\node[draw] (C) at (2.5,3) {};
\draw (A) -- (B.center) -- (C.north) -- (A);%连线可以连到字母⽅框的某个位置:center/north/south/east/west
\node[draw,anchor=east] (a) at (A) {$a_0$};%定义A的锚点在A位置的东部,然后定义a,并将其位置定义为A位置的中⼼    \node[draw,below right=4pt] (b) at (B) {b};%在相对于B的位置右下⽅4pt的地⽅定义b
\node[circle,fill=blue,text=white,font={\bfseries}]
(A) at (0,0) {A node};%填充、各种可⾃定义的参数
\node[rectangle,rounded corners,draw=gray,font={\sffamily\slshape}]
(B) at (2,0) {B node};
\node[draw] (P) at (-3,3){center};
\draw[dotted] (0,0)--(P.south);%画某点到某点的南边的线
\end{tikzpicture}
\end{document}
  效果图:
线段与点的标注
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}
\draw (2,1.5)coordinate (A)node[above] {$A$}%定义点A并标注A
--node[above left,sloped] {$c$}%给线标注c
(0,0)coordinate (B)node[left] {$B$}
--node[below]{$a$}
(2.5,0)coordinate (C)node[right]{$C$}
--node[right]{$b$}
cycle;
\end{tikzpicture}
\end{document}
  效果图:
例⼦
  代码:
\documentclass{article}
\usepackage{tikz}
\a}%画箭头⽤的包
\begin{document}
\begin{tikzpicture}[>=Stealth]
\draw[->,line width=0.2pt](-0.5,0)--(4.5,0);
\draw[->,line width=0.2pt](0,-0.5)--(0,2.5);
\coordinate (a) at (0.5,1.9);
\coordinate (b) at (4,1.2);
\coordinate (a0) at (a |- 0,0);
\coordinate (b0) at (b |- 0,0);
\node[below] at (a0) {$a$};
\node[below] at (b0) {$b$};
\filldraw[fill=gray!50,draw,thick]
(a0)--(a)..controls(1,2.8)and(2.7,0.4)..(b)--(b0)--cycle;
\node[above right,outer sep=0.2cm,rounded corners,fill = green!20,draw = black,text = blue!60!red,scale = 0.6] %blue60,red40        at (b) {$\displaystyle\int_a^bf(x)dx = F(b)-F(a)$};%写标注,draw边框,fill填充,scale字体⼤⼩
\end{tikzpicture}
\end{document}
  效果图:
⽤循环、判断语句绘图
  代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgffor}%可以使⽤foreach的包
\usepackage{ifthen}%可以使⽤ifthenelse的包,还能使⽤whiledo
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,5}{
\foreach \j in {0,...,\i}{
\ifthenelse{\i > 3}{%if成⽴
\node[fill = green!20,rounded corners]at (\i,\j) {(\i,\j)};
}{%if不成⽴
\node[fill = red!20,rounded corners]at (\i,\j) {(\i,\j)};
}
}
}
\end{tikzpicture}
\end{document}
  效果图:
定义变量、使⽤函数
  这个库可以让你使⽤过程语⾔结合LaTeX的画图脚本的来画图,⼗分⽅便。主要就是导⼊math库:  \usetikzlibrary{math}%使⽤数学程序
  \usetikzlibrary{fpu}%修理不能使⽤ifthenelse的错误
  代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}%修理不能使⽤ifthenelse的错误
\usetikzlibrary{math}%使⽤数学程序
\begin{document}
\begin{tikzpicture}
\tikzmath{ %数学程序编写,定义的变量可以在其外部使⽤,⾥⾯所有语句都要加分号; 并且不能有多余的回车
function paint_loop(\x,\y){ %函数中只能使⽤局部变量,外部变量不能⽤
for \i in {0,1,...,\x}{
for \j in {0,...,\y}{
\jj = \j;
if \ii > 2 then {%条件分⽀语句
{%绘图代码在数学程序内部使⽤,要⽤画括号括起来
\node[draw,fill = green!20,rounded corners] at (\ii,\jj) {(\ii,\jj)};
};
}else{
{
\node[draw,fill = red!20,rounded corners] at (\ii,\jj) {(\ii,\jj)};
};
};
};
};
};
\a = 5; %和python⼀样,加个.就是实数,不加点就是⼩数
\b = 3;
paint_loop(\a,\b);
coordinate \co;%能⾃定义的变量有int、real、coordinate(坐标),不能在定义的时候赋值        \co = (3,4.5);
}
\draw[-latex] (-1,0) -- (5,0);
\draw[-latex] (0,-1) -- (0,5);
\node at (\co) {COCOCO}; %在tikzmath中定义的变量在能外部使⽤
\end{tikzpicture}
\end{document}
  效果图:
绘制神经⽹络图  代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}%修理不能使⽤ifthenelse的错误
\usetikzlibrary{math}%使⽤数学程序
\begin{document}
\begin{tikzpicture}
\tikzmath{
function paint_nodes(\radius,\gapy,\posx,\num){
\gapy = \gapy+\radius*2;
\starty = \gapy*(\num-1)/2;
for \i in {0,...,\num-1}{
\drawy = \starty - \i*\gapy;
{
\filldraw[line width = 0.5pt,fill = white] (\posx,\drawy) circle (\radius);
};
};
};
function paint_lines(\radius,\gapy,\posx,\num,\nextposx,\nextnum){
\gapy = \gapy+\radius*2;
\starty = \gapy*(\num-1)/2;
\startyy = \gapy*(\nextnum-1)/2;
for \i in {0,...,\num-1}{
\drawy = \starty - \i*\gapy;
for \j in {0,...,\nextnum-1}{
\drawyy = \startyy - \j*\gapy;
radius软件{
\draw (\posx,\drawy) -- (\nextposx,\drawyy);
};
};
};
};
function paint_x_lines(\radius,\gapy,\posx,\num,\ifright,\len){
\gapy = \gapy+\radius*2;
\starty = \gapy*(\num-1)/2;
for \i in {0,...,\num-1}{
\drawy = \starty - \i*\gapy;
if \ifright == 1 then{
{
\draw[-latex] (\posx,\drawy) -- (\posx+\len,\drawy);
};
}else{
{
\draw[-latex] (\posx,\drawy)--(\posx-\len,\drawy);
};
};
};
};
function paint_net(\x0,\x1,\x2,\x3){
\gapx = 2;
paint_lines(\radius,\gapy,0*\gapx,\x0,1*\gapx,\x1);
paint_lines(\radius,\gapy,1*\gapx,\x1,2*\gapx,\x2);
paint_lines(\radius,\gapy,2*\gapx,\x2,3*\gapx,\x3);
paint_x_lines(\radius,\gapy,3*\gapx,\x3,1,1.8);
paint_x_lines(\radius,\gapy,0*\gapx-1,\x0,1,1);
paint_nodes(\radius,\gapy,1*\gapx,\x1);
paint_nodes(\radius,\gapy,2*\gapx,\x2);
paint_nodes(\radius,\gapy,3*\gapx,\x3);
};
paint_net(7,9,10,5);
}
\node[scale = 0.9] at (0,-4.2) {Feature};
\node[scale = 0.9] at (2,-4.2) {Input layer};
\node[scale = 0.9] at (4,-4.2) {Hide layer};
\node[scale = 0.9] at (6,-4.2) {Output layer};
\end{tikzpicture}
\end{document}
  效果图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。