2011年12月3日星期六

LaTex:Tikz画图


1、画虚线
\begin{tikzpicture}[dash pattern=on 2pt off 3pt on 4pt off 4pt]
\draw (0pt,0pt) -- (3.5cm,0pt);
\end{tikzpicture}

代码的意思是:on 2pt off 3pt on 4pt off 4pt means \draw 2pt, then leave out 3pt, then draw 4pt oncemore, then leave out 4pt again, repeat。

2、文字画边框
\tikzstyle{every node}=[fill=white,scale=0.8,draw]
    \draw (0, 0) node{1963};

3、节点的形状
需要增加命令\usetikzlibrary{arrows,snakes,backgrounds,shapes},其中shapes是跟节点形状相关的,其他则有备无患,例子
\begin{figure}[h]
  \centering
  \begin{tikzpicture}[fill=blue!20]
    \draw[style=help lines] (-1,-2) grid (6,3);
    \path (0,0) node(a) [ellipse,rotate=10,draw,fill] {An ellipse}
    (3,-1) node(b) [circle,draw,fill] {A circle}
    (2,2) node(c) [rectangle,rotate=20,draw,fill] {A rectangle}
    (5,2) node(d) [rectangle,rotate=-30,draw,fill] {Another rectangle};
    \draw[thick] (a.south) -- (b) -- (c) -- (d);
    \draw[thick,red,->] (a) |- +(1,3) -| (c) |- (b);
    \draw[thick,blue,<->] (b) .. controls +(right:2cm) and +(down:1cm) .. (d);
  \end{tikzpicture}
\end{figure}

4、画矩阵,箭头
\begin{figure}[h]
  \def\nscale{0.7}
  \centering
  \begin{tikzpicture}[scale=\nscale]     %控制图片缩放
  \tikzstyle{every node}=[scale=\nscale]
  \begin{scope}[line width=1pt]
    \tikzstyle{every node}=[ellipse, draw,scale=\nscale]
    \tikzstyle{pre}=[<-,shorten <=1pt,>=stealth',semithick]    % 箭头设置,好看一点
    \tikzstyle{post}=[->,shorten >=1pt,>=stealth',semithick]   %
    \path (0,2) node(v0) {多云~(C)};
    \path (2,0) node(v1) {下雨~(R)};
    \path (-2,0) node(v2) {洒水~(S)};
    \path (0,-2) node(v3) {湿的玻璃~(W)};
    \draw[post] (v0) to (v1);
    \draw[post] (v0) to (v2);
    \draw[post] (v1) to (v3);
    \draw[post] (v2) to (v3);
  \end{scope}
  \begin{scope}
  \matrix [] at (0,3.5)
    { \node {\nm{P(C=F)}}; & \node {\nm{P(C=T)}}; \\
      \hline
      \node {\nm{0.5}}; & \node {\nm{0.5}};\\
    };
  \matrix at (-5.5,0)
    { \node {\nm{C}}; & \node {\nm{P(S=F)}}; & \node {\nm{P(S=T)}}; \\
      \hline
      \node {\nm{F}}; & \node {\nm{0.5}}; & \node {\nm{0.5}}; \\
      \node {\nm{T}}; & \node {\nm{0.9}}; & \node {\nm{0.1}}; \\
    };
  \matrix at (5.5,0)
    { \node {\nm{C}}; & \node {\nm{P(R=F)}}; & \node {\nm{P(R=T)}}; \\
      \hline
      \node {\nm{F}}; & \node {\nm{0.8}}; & \node {\nm{0.2}}; \\
      \node {\nm{T}}; & \node {\nm{0.2}}; & \node {\nm{0.8}}; \\
    };
  \matrix at (0,-4)
    { \node {\nm{S}}; & \node {\nm{R}}; & \node {\nm{P(R=F)}}; & \node {\nm{P(R=T)}}; \\
      \hline
      \node {\nm{F}}; & \node {\nm{F}}; & \node {\nm{1.0}}; & \node {\nm{0.0}}; \\
      \node {\nm{T}}; & \node {\nm{F}}; & \node {\nm{0.1}}; & \node {\nm{0.9}}; \\
      \node {\nm{F}}; & \node {\nm{T}}; & \node {\nm{0.1}}; & \node {\nm{0.9}}; \\
      \node {\nm{T}}; & \node {\nm{T}}; & \node {\nm{0.01}}; & \node {\nm{0.99}}; \\
    };
  \end{scope}
  \end{tikzpicture}
  \caption{一个简单的贝叶斯网络}\label{fig:example1}
\end{figure}


参考文档:

没有评论:

发表评论