初学Tikz
作图的朋友也许觉得给图形填充颜色很费事儿,尤其是那些不规则图形的颜色填充。
事实上,只要你掌握一个基本思想和三个填充技巧,图形颜色填充就会变得易如反掌!下面我们详细介绍这些方法,学会本文中的各个例子,再复杂的颜色填充问题都不在话下了!
本文较长,需要的朋友可以收藏备查!
基本思想
由曲线段(折线段)首尾相连得到的封闭图形都可以通过命令\filldraw[fill=<color>]
将它填充为设定的颜色<color>
。
值得注意的是,如果折线段(曲线段)的起点和终点没有连起来,使用上述命令填充颜色时, Tikz
会自动将起点和终点用线段连结从而将其封闭,然后用设定的颜色填充。例如,一段圆弧使用上述命令时,填充以后得到一个弓形。
下面的例子,就是用这种基本思想实现的。
初学者掌握这个基本思想,就可以应付一些简单图形的颜色填充了。
如果需要对一些复杂的不规则图形进行填充则需要学习下面的三大填充技巧。
填充技巧一:剪裁(clip)
命令\clip
顾名思义就是“剪裁”的意思,它和\draw
的区别就是:它不画出后面的路径,而是用这个路径对后面的作图进行剪裁。
说得通俗一点,例如\clip(-1,-1)rectangle(1,1)
就是剪出一块正方形的画布,后面所有的作图命令作出的图形将只显示这个正方形内部的部分。
注意:
- 如果要画出剪裁的路径可以在命令
\clip
后面添加draw
选项,例如,
\clip[draw](-1,-1)rectangle(1,1)
。 - 如果希望剪裁命令只是作用在某个局部,那么使用
scope
环境将它们包裹起来。
下面的例子给我们直观地显示\clip
命令的效果:
\begin{tikzpicture}
\draw[fill=blue02,opacity=0.9](-1,-1)rectangle(1,1);
\draw[fill=red01,opacity=0.9](-1,0)circle(1);
\begin{scope}
\clip[draw](3,-1)rectangle(5,1);
\draw[fill=red01,opacity=0.9](3,0)circle(1);
\end{scope}
\begin{scope}
\clip[draw](8,0)circle(1);
\draw[fill=blue02,opacity=0.9](8,-1)rectangle(10,1);
\end{scope}
\end{tikzpicture}
图形:
例1
\begin{tikzpicture}
\begin{scope}
\clip(0,-1)circle(1);
\draw [draw=gray,fill=red!30](-1,-1)arc(-90:0:1)--(-1,0)--cycle;
\draw[draw=gray,fill=red!30](0,0)arc(180:270:1)--(1,0)--cycle;
\end{scope}
\begin{scope}[rotate=180]
\clip(0,-1)circle(1);
\draw [draw=gray,fill=red!30](-1,-1)arc(-90:0:1)--(-1,0)--cycle;
\draw[draw=gray,fill=red!30](0,0)arc(180:270:1)--(1,0)--cycle;
\end{scope}
\clip[draw](-1,-1)rectangle(1,1);
\draw[gray](0,-1)circle(1)(0,1)circle(1);
\end{tikzpicture}
图形:
填充技巧二:奇偶性法则(even odd rule)
奇偶性法则是颜色填充的黄金法则。妙用奇偶性法则可以大大简化作图代码。
奇偶性法则:使用\filldraw[fill=<color>,even odd rule]
命令作图时,如果选项里有even odd rule
, 那么
当几条路径有重叠时,那么重叠部分属于奇数条路经时将被着色,重叠部分属于偶数条路经时将不被着色。
一个简单容易理解的例子如下:
例2
\begin{tikzpicture}[scale=1,
pattern1/.style={draw=red,pattern color=red!60,pattern=north east lines},
]
\filldraw[thick,pattern1,even odd rule](0,0)circle(1.5 cm)(2,0)circle(1.5 cm);
\end{tikzpicture}
图形:
例3 用奇偶性法则重画例1.
\begin{tikzpicture}
\filldraw[fill=orange!30,even odd rule](-1,-1)rectangle(1,1)(-1,-1)rectangle(1,1)(0,0)arc(0:-90:1)arc(180:90:1)
arc(180:270:1)arc(0:90:1)arc(180:90:1)arc(360:270:1)arc(0:90:1)arc(180:270:1);
\end{tikzpicture}
图形:
例4 太极图
\begin{tikzpicture}
\filldraw [draw=black,fill=black, even odd rule](2,0)arc[radius=2cm,start angle=0,end angle=180](0,0)arc[radius=1cm,start angle=0,end angle=180](0,0)arc[radius=1cm,start angle=180,end angle=360](1,0)circle(5pt);
\draw(0,0)circle(2cm);
\draw[fill=black](-1,0)circle(5pt);
\end{tikzpicture}
图形:
填充技巧三:图层(layer)
图层的技巧很容易理解,后画的图形着色将覆盖先画的图形的着色。利用这种特点可以非常方便地实现不规则图形的颜色填充。
如果要使重叠区域的几种颜色混合,可以设置填充颜色的透明度:例如设置opacity=0.6
,那么重叠区域的颜色将以混合后的颜色显示。
例5
\tikzset{help line/.style={gray,thin},every path/.style={draw}}
\begin{tikzpicture}
%\draw[help line](-2.5,-2.5)grid(2.5,2.5);
% \draw(-2.5,0)--(2.5,0)(0,-2.5)--(0,2.5);
\draw[fill=red01,opacity=0.8](-1.3,0)circle(1.8);
\draw[fill=blue03,opacity=0.8](1.3,0)circle(1.8);
\draw[fill=blue02,opacity=0.8](0,1.3)circle(1.8);
\draw[fill=yellow01,opacity=0.8](0,-1.3)circle(1.8);
\node at (-2.2,0){\color{white}1};
\node at (-1,1){\color{white}2};
\node at (0,2){\color{white}3};
\node at (1,1){\color{white}4};
\node at (2.2,0){\color{white}5};
\node at (0,0.8){\color{white}6};
\node at (-0.8,0){\color{white}7};
\node at (0,0){\color{white}8};
\node at (0.8,0){\color{white}9};
\node at (1,-1){\color{white}10};
\node at (0,-0.8){\color{white}11};
\node at (-1,-1){\color{white}12};
\node at (0,-2){\color{white}13};
\end{tikzpicture}
图形:
例6
\begin{tikzpicture}[every path/.style={draw}]
%\draw[help line] (-5,-5)grid(5,5);
% \draw (-5,0)--(5,0)(0,-5)--(0,5);
\begin{scope}[rotate=45]
\draw[fill=pink01,opacity=0.8](0,0)ellipse[x radius=2,y radius=1];
\draw[fill=green01,opacity=0.8](-0.3,1)ellipse[x radius=1.9,y radius=0.8];
\end{scope}
\begin{scope}
\draw[rotate around={135:(-2,0)},fill=green02,opacity=0.6](-2,0)ellipse[x radius=2,y radius=1];
\draw[rotate around={135:(-2,0)},fill=blue01,opacity=0.6](-2.3,-1)ellipse[x radius=1.9,y radius=0.8];
\end{scope}
\node at (-1.6,1.3){1};
\node at (-1,1){2};
\node at (-0.4,1.3){3};
\node at (-2,0.8){4};
\node at (-1.5,0.5){5};
\node at (-1,-0.2){6};
\node at (-0.5,0.5){7};
\node at (0,0.8){8};
\node at (0.8,0.5){9};
\node at (0,-0.5){10};
\node at (-1,-1.2){11};
\node at (-1.9,-0.5){12};
\node at (-2.8,0.5){13};
\end{tikzpicture}
图形:
填充技巧四:以图案填充(patterns)
在作一些与面积有关的平面图形以及在画表示集合的交、并、补等关系的韦恩图时,常常使用斜线填充某些部分,这是用图案填充的例子。利用Tikz
预定义好的几十种图案可以非常方便的实现图案填充。
使用图案填充的要点有:
- 使用图案填充时,要调用
patters
库,就是在导言区添加下面的语句:
\usetikzlibrary{patterns}
. - 选用某种图案:
pattern=<name>
- 设置图案颜色:
pattern color=<color>
- 常见的图案选项有:
名称 | 形状 |
---|---|
horizontal lines | 水平线 |
vertical lines | 竖直线 |
north east lines | 右上-左下斜线 |
north west lines | 左上-右下斜线 |
grid | 水平格子 |
crosshatch | 倾斜格子 |
dots | 水平点 |
crosshatch dots | 斜点 |
fivepointed stars | 五角星 |
sixpointed stars | 六角星 |
bricks | 砖形 |
checkerboard | 棋盘形状 |
例7 斜线填充例1中的图形
\begin{tikzpicture}
\filldraw[pattern color=red!30,pattern=,even odd rule](-1,-1)rectangle(1,1)(-1,-1)rectangle(1,1)(0,0)arc(0:-90:1)arc(180:90:1)
arc(180:270:1)arc(0:90:1)arc(180:90:1)arc(360:270:1)arc(0:90:1)arc(180:270:1);
\end{tikzpicture}
图形:
例8 砖形图案填充
本例来自pgfmanual
说明文档。
\begin{tikzpicture}
\def\mypath{(0,0) -- +(0,1) arc (180:0:1.5cm) -- +(0,-1)}
\fill [red!80!black] \mypath;
\pattern[pattern color=white,pattern=bricks] \mypath;
\end{tikzpicture}
图形:
填充技巧五:以渐变色填充(shade)
使用渐变色填充的两个基本命令是\shade
和\shadedraw
, 两者的区别是前者只用渐变色填充而不画出路径(轮廓),后者是先填充然后画出路径(轮廓)。
使用渐变填充也要先调用库文件,即在导言区添加命令:\usetikzlibrary{shadings}
。
常用的渐变填充有下面四种:
- (1)线性
axis
- (2)辐射:
radical
- (3) 球形:
ball
- (4) 色轮:
colorwheel
前三种默认填充颜色都是“灰-白”渐变,线性填充默认为从上到下渐变。
其它填充选项见下表:
例9 渐变色填充举例
本例来自pgfmanual
说明文档。
\begin{tikzpicture}
\draw[top color=red] (0,0) rectangle (2,1);
\draw[bottom color=red] (3,0) rectangle (5,1);
\draw[middle color=red] (6,0) rectangle (8,1);
\draw[left color=red] (9,0) rectangle (11,1);
\draw[right color=red] (12,0) rectangle (14,1);
\draw[inner color=red] (0,-2) rectangle (2,-1);
\draw[outer color=red] (3,-2) rectangle (5,-1);
\shade[shading=color wheel black center](7,-2)circle(1.5);
\shade[shading=color wheel white center](10.5,-2)circle(1.5);
\end{tikzpicture}
图形:
今天的介绍就到这里,喜欢的话欢迎点赞、留言和分享,谢谢啦!
【LaTeX微信交流加群:】
如果你希望交流 LaTeX
使用或者Tikz
作图或者Beamer
制作幻灯片方面的问题,请先添加作者微信:niltxz
,然后加入作者的LaTeX+TikZ+Beamer
交流微信群。