一些tikz绘图例子

tikz的一些画图例子

前言

在此记录一些tikz绘制图形的例子,以方便后续使用。

例子1

代码如下:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{intersections}
\begin{tikzpicture}[scale=3]
%\clip[draw] (-0.8,-0.2) rectangle (1.1,1.75);
\draw[->] (-1.5, 0) -- (1.5,0)node [right] {$x$} coordinate (x axis);
\draw[->] (0, -1.5) -- (0,1.5)node [above] {$y$} coordinate (y axis);
\draw (0,0) circle [radius=1cm];

\draw[step=0.5cm,gray,very thin] (-1.4, -1.4) grid  (1.4,1.4) ;
\filldraw[fill=green!20!white, draw=green!50!black] (0mm,0mm) -- (3mm,0mm) arc [start angle=0,end angle=30,radius=3mm] -- cycle;
\draw (15:2mm) node {\color{green!50!black}$\alpha$};
\draw[red,very thick] (30:1cm) -- node[left=1pt,fill=white]{$\sin\alpha$} (30:1cm |- x axis);
\draw[blue,very thick] (30:1cm |- x axis) -- node[below=1pt,fill=white]{$\cos\alpha$} (0,0);

\path[name path=upward line] (1,0) -- (1,1);
\path[name path=sloped line] (0,0) -- (30:2cm);
\draw[name intersections={of=upward line and sloped line, by=x}] [very thick, orange] (1,0) --
      node[right=1pt,fill=white]{$\tan\alpha=\frac{\color{red}\sin\alpha}{\color{blue}\cos\alpha}$} (x);

\draw (0,0) -- (x);
\foreach \x/\xtext in {-1/-1, -0.5/-\frac{1}{2}, 1}
 \draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor=north,fill=white] {$\xtext$};

\foreach \y/\ytext in {-1,-0.5/-\frac{1}{2},0.5/\frac{1}{2},1}
 \draw ( 1pt,\y cm) -- (-1pt, \y cm) node[anchor=east,fill=white] {$\ytext$};

\end{tikzpicture}
\end{document}

生成图形如下:

点击下载源文件

例子2

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary[arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri]
\begin{tikzpicture}
[bend angle=45, node distance=1.3cm, on grid, auto,
place/.style={circle, draw=blue!50,fill=blue!20, thick, inner sep=0pt, minimum size=6mm},
red place/.style={place, draw=red!75, fill=red!20},
transition/.style={rectangle, draw=black!50,fill=black!20, thick, inner sep=0pt, minimum size=4mm},
every label/.style={red},
pre/.style={->, shorten >=1pt, >={Stealth[round]}, semithick},
post/.style={<-, shorten <=1pt, >={Stealth[round]}, semithick}] % 预定义样式

\node [place, tokens=1] (a1) {};
\node [place] (a2) [below of=a1] {};
\node [place] (a3) [below of=a2, label=above:$\color{red}s\leq 3$] {};
\node [transition] (b1) [left of=a2] {};
\node [transition] (b2) [right of=a2] {};
\draw (a1) edge [pre, bend right] (b1) edge [post, bend left] node [auto] {2} (b2);
\draw (a3) edge [pre, bend right] (b2) edge [post, bend left] (b1);
\draw (a2) edge [post] (b1) edge [pre] (b2);

\node [place] (a4) [below of=a3] {};
\node [place] (a5) [below of=a4, tokens=1] {};
\node [transition] (b3) [left of=a4] {} edge [pre, bend left] (a3) edge [pre] (a4) edge [post, bend right] (a5);
\node [transition] (b4) [right of=a4] {} edge [pre, bend left] node [auto] {2} (a5) edge [post, bend right] (a3) edge [post] (a4);

\begin{scope}[xshift=6cm]
\node [place,tokens=1] (a1') {};
\node [place] (a2') [below of=a1'] {};
\node [transition] (b1') [left of=a2'] {} edge [post, bend left] node [auto,swap] {2} (a1') edge [pre] (a2');
\node [transition] (b2') [right of=a2'] {} edge [pre, bend right] (a1') edge [post] (a2');

\node [red place] (a31') [below of=a2', xshift=-5mm, label=left:$\color{red}s$] {} edge [post] (b1') edge [pre] (b2');
\node [red place, tokens=3] (a32') [below of=a2', xshift=5mm, label=right:$\color{red}\bar{s}$] {} edge [post] (b2') edge [pre] (b1');

\node [place] (a4') [below of =a31',xshift=5mm] {};
\node [transition] (b3') [left of=a4'] {} edge [pre] (a31') edge [pre] (a4') edge [post] (a32');
\node [transition] (b4') [right of=a4'] {} edge [pre] (a32') edge [post] (a31') edge [post] (a4');

\node [place,tokens=1] (a5') [below of=a4'] {} edge [post,bend right] node [auto] {2} (b4') edge [pre, bend left] (b3');
\end{scope}

\begin{scope}[on background layer]
\node (r1) [fill=black!10, rounded corners,fit=(a1)(a2)(b1)(b2)(a3)(a4)(b2)(b3)(a5)] {};
\node (r2) [fill=black!10, rounded corners,fit=(a1')(a2')(b1')(b2')(a31')(a32')(a4')(b2')(b3')(a5')] {};
\end{scope}

\draw [shorten >=1mm,-to,thick,decorate, decoration={snake,amplitude=0.4mm,segment length=2mm, pre=moveto,pre length=1mm,post length=2mm}]
(r1) -- (r2) node [above=1mm,midway,text width=3cm,align=center]
{replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};


\end{tikzpicture}
\end{document}

生成图形如下:

点击下载源文件

例子3

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{backgrounds, calc, intersections, through}
\begin{tikzpicture}
\coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0,0);
\coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1.25, 0.25);
\draw (A) edge [blue] (B);
\node [name path=D,draw,label=left:$D$] (D) at (A) [circle through=(B)] {};
\node [name path=E,draw,label=right:$E$] (E) at (B) [circle through=(A)] {};
\path [name intersections={of=D and E,by={[label=above:\textcolor{red}{$C$}]C, [label=below:\textcolor{red}{$C'$}]C'}}];
\draw (C) edge [red] (A) edge[red] (B);
\foreach \point in {A,B,C}
\fill [black, opacity=0.5] (\point) circle (2pt);
\begin{pgfonlayer}{background}
\fill [orange!80] (A) -- (B) -- (C) -- cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

生成图形如下:

点击下载源文件