TikZ 绘图学习笔记
这篇笔记的所有代码如下:
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of document.
\documentclass[11pt]{article} % use larger type; default would be 10pt
\usepackage[UTF8, scheme = plain]{ctex} % set input encoding (not needed with XeLaTeX)
%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.
%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{
a4paper, % 设置纸张为A4大小
total={170mm,257mm}, % 设置文本区域的总宽度和高度
left=25mm, % 设置左侧边距
top=25mm, % 设置上侧边距
headheight=15mm, % 设置页眉高度
headsep=10mm % 设置页眉与文本之间的间距
}
\usepackage{graphicx} % support the \includegraphics command and options
% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent
%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
% These packages are all incorporated in the memoir class to one degree or another...
%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}
%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,through,decorations.pathmorphing, arrows.meta,quotes,mindmap,shapes.symbols,shapes.arrows,automata,angles,3d,trees,shadows,automata,arrows,shapes.callouts}
\usepackage{amsmath} % 导入amsmath宏包
\usepackage{caption}
% 设置caption宏包,改变图注格式,显示为图X,而不是Figure X
\captionsetup[figure]{
%labelfont=bf, % 图注标签加粗
name={图} % 设置图注前缀为“图”
}
%以下是algorithim,控制多行代码显示
\usepackage{algorithm}
\makeatletter
\newenvironment{breakablealgorithm}
{% \begin{breakablealgorithm}
\begin{center}
\refstepcounter{algorithm}% New algorithm
\hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
\renewcommand{\caption}[2][\relax]{% Make a new \caption
{\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
\ifx\relax##1\relax % #1 is \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
\else % #1 is not \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
\fi
\kern2pt\hrule\kern2pt
}
}{% \end{breakablealgorithm}
\kern2pt\hrule\relax% \@fs@post for \@fs@ruled
\end{center}
}
\makeatother
\usepackage{minted} %使用minted库高亮代码
%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!
%%% END Article customizations
%%% The "real" document content comes below...
\title{\texttt{TikZ}\ 学习篇}
\author{蒟蒻小史}
%\date{} % Activate to display a given date or no date (if empty),
% otherwise the current date is printed
\begin{document}
\setlength{\parindent}{0pt} % 全局禁用首行缩进
\maketitle
\section{画一条直线}
这里是画了一条从(0,0)到(3,3)的直线。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw (0,0) -- (3,3);
\end{tikzpicture}
\caption{从(0,0)到(3,3)的直线}
\label{fig:diagram1}
\end{figure}
再画两条直线,分别是(0,0)到(3,3)和(1,2)到(3,6)。这里是在同一个坐标内画的图。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw (0,0) -- (3,3);
\draw (1,2) -- (3,6);
\end{tikzpicture}
\caption{同一坐标系中两条直线}
\label{fig:diagram2}
\end{figure}
接下来画个复杂一点的。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color=blue!50,->](0,0) node[left]{$A$}-- node [color=red!70,pos=0.5,above,sloped]{Hello}(3,3) node[right]{$B$};
\end{tikzpicture}
\caption{复杂一点的图}
\label{fig:diagram3}
\end{figure}
$color=blue!50$ 表示使用 $50\%$ 的蓝色,因为 LaTeX 中,\% 用作注释,所以这里用 ! 替代。
$->$ 表示的是线形是一个箭头。
我们注意到,在起点坐标,$-–$,终点坐标后面,我们分别加入了一个 $\text{node}$ 元素,起点后面的 $\text{node}$ 表示的是加入一个标示,它在坐标点 $(0,0)$ 的左边,$--$ 后面的 $\text{node}$ 采用 $70\%$ 的红色,位置在线段的上方 $0.5$ 的位置,随线段倾斜,花括号中是 $\text{node}$ 的文字,为 $\text{Hello}$,终点坐标同理。
$\text{node}$ 经常用于加入一些标注。
\section{画圆}
画一个圆心在原点,半径为 $10pt$ 的圆。
画一个圆心在原点,半径为 $20pt$ 的红色圆。
画一个圆心在原点,半径为 $30pt$ 的绿色圆,其中线条为2pt宽。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw (0,0) circle (10pt);
\draw [color = red!50] (0,0) circle (20pt);
\draw [color = green!50, line width=2pt] (0,0) circle (30pt);
\end{tikzpicture}
\caption{画圆}
\label{fig:diagram4}
\end{figure}
\section{画椭圆}
画一个中心在原点,长轴、短轴分别为20pt和10pt的椭圆,并填充绿色,黑边;
画一个中心在原点,长轴、短轴分别为40pt和20pt的红色椭圆;
画一个中心在原点,长轴、短轴分别为20pt和40pt的蓝色椭圆;
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\filldraw[fill=green!20!white, draw=green!50!black] (0,0) ellipse (20pt and 10pt);
\draw [color = red!50] (0,0) ellipse (40pt and 20pt);
\draw [color = blue!50] (0,0) ellipse (20pt and 40pt);
\end{tikzpicture}
\caption{画椭圆}
\label{fig:diagram5}
\end{figure}
注意:$\text{fill=green!20!white}$ 的含义是创建一个颜色,它是 20\% 的绿色和 80\% 的白色的混合色。这种颜色混合是通过 \text{TikZ} 的颜色计算自动完成的,结果是一种介于绿色和白色之间的颜色,具体来说是一种较浅的绿色。
\section{画多边形}
\subsection{画四边形}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) -- (0,1) -- (1,1) -- (1,0) --cycle ;
\end{tikzpicture}
\caption{四边形}
\label{fig:diagram6}
\end{figure}
\subsection{画五边形}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) -- (0,1) -- (1,3) -- (2,3) -- (4,0) --cycle ;
\end{tikzpicture}
\caption{五边形}
\label{fig:diagram7}
\end{figure}
\newpage
\subsection{画矩形}
对于矩形来说,我们还有更加简化的代码,我们可以通过命令 $\text{Rectangle}$(关键词)来实现。一般在绘制矩形时我们会确定两个坐标(左下和右上);对角线上两端点的坐标。并且可以通对角线上的坐标来改变矩形的形状。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) rectangle (2,2) ;
\end{tikzpicture}
\caption{矩形}
\label{fig:diagram8}
\end{figure}
\section{画曲线}
Tikz 在两点除了能绘制直线段之外,我们还可以进行绘制曲线(抛物线)。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) parabola (5,2);
\end{tikzpicture}
\caption{两点间曲线}
\label{fig:diagram9}
\end{figure}
另外我们还可以通过点控制来绘制具有多个方向的曲线,利用关键词 $\text{control}$ 来引导曲线的走向,横纵坐标的大小会控制曲线转向的程度,空点的位置会确定曲线的方向。可以说成是 $\text{control}$ 点控制起到了绘制曲线方向的引导作用。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) .. controls (1,1) and (4,3) .. (5,2);
\end{tikzpicture}
\caption{多点曲线1}
\label{fig:diagram10}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (0,0) .. controls (2,-3) and (4,3) .. (5,2);
\end{tikzpicture}
\caption{多点曲线2}
\label{fig:diagram11}
\end{figure}
\newpage
$\text{Bézier curve}$ (贝塞尔曲线)是应用于二维图形应用程序的数学曲线。 曲线定义:起始点、终止点(也称锚点)、控制点。通过调整控制点,贝塞尔曲线的形状会发生变化,如弹弓线,具体见:https://blog.csdn.net/sangxiaonian/article/details/51984013。
画一个起点为(1,3),终点为(7,4),控制点为(2,-1),(4,-3)的贝塞尔曲线。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = blue!50, line width=2pt] (1,3) .. controls (2,-1) and (4,-3) .. (7,4);
\end{tikzpicture}
\caption{贝塞尔曲线}
\label{fig:diagram12}
\end{figure}
注意,用 $\text{control}$ 来引导曲线的走向时,只能有两个控制点,如果多个控制点,就得需要多条贝塞尔曲线来拟合。
\section{画弧线}
圆弧的绘制,我们首先是先确定这条弧起点坐标,然后确定初始初始角的大小,最后的参量是这条圆弧对应的圆的半径大小。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw [color = red!50, very thick, scale = 1 ] (3,0) arc (0 : 75 : 3cm);
\end{tikzpicture}
\caption{从(3,0)点出发,半径为3cm的75度弧线}
\label{fig:diagram13}
\end{figure}
\section{简体组合成立体图形}
画一个圆和一个椭圆,注意这里椭圆是用两条弧线完成的,因为一条是实线,一条是虚线。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw[red, thick, scale=1] (3,3) node[left]{$O$} circle (3cm);
\draw[violet, dashed, scale=1] (6,3) node[right]{$B$} arc (0 : 180 : 3cm and 1cm) node[left]{$A$};
\draw[violet, scale=1] (6,3) arc (360 : 180 : 3cm and 1cm);
\end{tikzpicture}
\caption{圆和椭圆组成的立体图形}
\label{fig:diagram14}
\end{figure}
画一个边长为 $3cm$ 的立方体。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\draw[ thick, scale=1] (1,1) -- (1,4) -- (4,4) -- (4,1) -- cycle;
\draw[dashed, scale=1] (1,1) -- (2.466,2.5) -- (4,2.5) -- (5.466,2.5);
\draw[thick, scale=1] (5.466,2.5) -- (4,1) ;
\draw[thick, scale=1] (1,4) -- (2.466,5.5) -- (5.466,5.5) -- (4,4);
\draw[thick, scale=1] (5.466,5.5) -- (5.466,2.5);
\draw[dashed, scale=1] (2.466,5.5) -- (2.466,2.5) ;
\end{tikzpicture}
\caption{立方体}
\label{fig:diagram15}
\end{figure}
\section{网格}
在 TikZ 中绘制网格通常涉及到定义网格的范围和样式。以下是一些关键参数和它们的作用,以及如何使用它们来绘制网格:
\begin{itemize}
\item 网格范围:左下角和右上角的坐标,这两个点定义了网格的边界。左下角的坐标确定了网格的起始点,而右上角的坐标确定了网格的结束点。
\item 网格样式
\begin{itemize}
\item 线型:可以设置网格线是实线、虚线还是点线等。
\item 颜色:可以定义网格线的颜色。
\item 间距:网格线之间的距离,可以是固定的,也可以是随着网格大小变化而变化的。
\end{itemize}
\item grid:TikZ 的一个库,提供了方便绘制网格的选项。
\end{itemize}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
\draw[step=2cm, gray, very thin, scale=1] (0,0) node[left]{$A$} grid (8,8) node[right]{$B$} ;
\end{tikzpicture}
\caption{网格}
\label{fig:diagram16}
\end{figure}
接下来,我们绘制一个填充单一颜色的网络。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
\draw[step=2cm, gray, very thin, scale=1] (0,0) node[left]{$A$} grid (8,8) node[right]{$B$} ;
\filldraw [black!50!white] (0,0) rectangle (2,2) ;
\filldraw [black!50!white] (4,0) rectangle (6,2) ;
\filldraw [black!50!white] (2,2) rectangle (4,4) ;
\filldraw [black!50!white] (6,2) rectangle (8,4) ;
\filldraw [black!50!white] (0,4) rectangle (2,6) ;
\filldraw [black!50!white] (4,4) rectangle (6,6) ;
\filldraw [black!50!white] (2,6) rectangle (4,8) ;
\filldraw [black!50!white] (6,6) rectangle (8,8) ;
\end{tikzpicture}
\caption{填充颜色的网格}
\label{fig:diagram17}
\end{figure}
\newpage
在颜色填充过程中,与上面填充方式不同的是,我们可以渐变色填充,通过命令 shade,其中可以设置$<top color>=color$ 和 $ <bottom color>=color$ 或者$<left color>=color$ 和 $ <right color>=color$,只需要设置相对的两个方向的颜色,如图 \ref{fig:diagram18}。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
\draw[step=2cm, gray, very thin, scale=1] (0,0) node[left]{$A$} grid (8,8) node[right]{$B$} ;
\shade[left color=red, right color=green,scale=1] (2,2) rectangle (6,6);
\end{tikzpicture}
\caption{渐变色填充网格}
\label{fig:diagram18}
\end{figure}
%\newpage
\section{笛卡尔坐标系(二维)}
先来绘制一个简单的笛卡尔坐标系。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
% 绘制坐标轴
\draw[->] (-4,0) -- (4,0) node[right] {$x$};
\draw[->] (0,-4) -- (0,4) node[above] {$y$};
% 绘制网格
\foreach \x in {-3,-2,-1,1,2,3} {
\draw (\x,-0.1) -- (\x,0.1) node[anchor=north] {$\x$};
\draw (-0.1,\x) -- (0.1,\x) node[anchor=east] {$\x$};
}
% 可选:绘制原点
\fill (0,0) circle (2pt) node[below right] {$O$};
\end{tikzpicture}
\caption{平面直角坐标系}
\label{fig:diagram19}
\end{figure}
%\newpage
再来绘制一个复杂一点的坐标系。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.5]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\end{tikzpicture}
\caption{平面直角坐标系}
\label{fig:diagram20}
\end{figure}
以上坐标系中,用到了 \text{foreach} 循环语句。
\begin{breakablealgorithm}
\caption{foreach块}
\label{code:longcpp}
\begin{minted}[breaklines]{latex}
\foreach \x in {1,2,...,4}{
执行语句;
};
\end{minted}
\end{breakablealgorithm}
\section{绘制函数}
下面我们进入函数世界。
\subsection{幂函数}
先来绘制一次函数。
\[
f(x) = x
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
% 绘制坐标轴
\draw[->] (-4,0) -- (4,0) node[right] {$x$};
\draw[->] (0,-4) -- (0,4) node[above] {$y$};
% 绘制网格
\foreach \x in {-3,-2,-1,1,2,3} {
\draw (\x,-0.1) -- (\x,0.1) node[anchor=north] {$\x$};
\draw (-0.1,\x) -- (0.1,\x) node[anchor=east] {$\x$};
}
% 可选:绘制原点
\fill (0,0) circle (2pt) node[below right] {$O$};
\draw[thick,domain=-3.5:3.5] plot(\x,\x) node[right]{$f(x)=x$};
\end{tikzpicture}
\caption{$f(x)=x$}
\label{fig:diagram21}
\end{figure}
\begin{itemize}
\item \text{thick}:这是一个线宽选项,用于设置线条的粗细。
\item \text{domain=-3.5:3.5}:这个选项设置了函数图的 $x$ 值范围,从 $-3.5$ 到 $3.5$。这意味着 \text{TikZ} 将在这个区间内绘制函数 $f(x) = x$ 的图形。
\item \text{plot(\textbackslash{x},\textbackslash{x})}:这个命令告诉 \text{TikZ} 绘制一个函数图,其中 \textbackslash{x} 是自变量,也用作因变量(即 $y$ 值),所以这里绘制的是一条通过原点的直线,斜率为 $1$。
\end{itemize}
再来绘制一下以下函数:
\[
f(x)=\frac{1}{3} x+2
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\draw[thick,domain=-5:5] plot(\x,{(\x)*(1/3) + 2}) node[right]{$f(x)=\frac{1}{3} x+2$};
\end{tikzpicture}
\caption{$f(x)=\frac{1}{3} x+2$}
\label{fig:diagram22}
\end{figure}
\newpage
下面我们来绘制函数:
\[
f(x) = x^n \qquad n \in \left\{-1, \frac{1}{3}, \frac{1}{2}, 1, 2, 3\right\}
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\draw[black,thick,domain=-0.25:-5] plot(\x,{1/(\x)}) node[left]{$f(x)=\frac{1}{x}$}; %这里区间要按两个写,因为x=0时报错。
\draw[black,thick,domain=0.25:5] plot(\x,{1/(\x)}) node[right]{$f(x)=\frac{1}{x}$}; %这里区间要按两个写,因为x=0时报错。
\draw[gray,thick,domain=-1.7:1.7] plot({(\x)^3},\x) node[right]{$f(x)=x^\frac{1}{3}$}; %同样要注意取值范围
\draw[yellow,thick,domain=0:2.5] plot({(\x)^2},\x) node[right]{$f(x)=x^\frac{1}{2}$}; %同样要注意取值范围
\draw[red,thick,domain=-5:5] plot(\x,\x) node[right]{$f(x)=x$};
\draw[blue,thick,domain=-2.5:2.5] plot(\x,{(\x)^2}) node[right]{$f(x)=x^2$};
\draw[green,thick,domain=-1.8:1.8] plot(\x,{(\x)^3}) node[left]{$f(x)=x^3$};
\end{tikzpicture}
\caption{$f(x)=x^n$}
\label{fig:diagram23}
\end{figure}
\subsection{指数函数和对数函数}
先来看指数函数:
\[
f(x) = \alpha ^x
\]
分别画出当$ \alpha = \frac{1}{2},2,3$时的曲线。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\draw[black,thick,domain=3.7:-2.2] plot(\x,{2^(-\x)}) node[left]{$f(x)=\frac{1}{2}^x$};
\draw[blue,thick,domain=-3.7:2.2] plot(\x,{2^(\x)}) node[right]{$f(x)=2^{x}$};
\draw[red,thick,domain=-2:1.5] plot(\x,{3^(\x)}) node[right]{$f(x)=3^{x}$} ;
%\node[red] at (-2, -1) {$f(x) = 3^x$};
\end{tikzpicture}
\caption{$f(x)=\alpha ^x $}
\label{fig:diagram24}
\end{figure}
\newpage
接下来画一下对数
\[
f(x) = \log_n{x}
\]
分别画出当$n= \frac{1}{2},2,10$时的曲线。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\draw[yellow!70!blue,thick,domain=3.7:-2.2] plot({2^(-\x)},\x) node[right]{$f(x)=\log_{\frac{1}{2}}{x}$};
\draw[green!70!black,thick,domain=-3.7:2.2] plot({2^(\x)},\x) node[right]{$f(x)=\log_2{x}$};
\draw[red!70!yellow,thick,domain=-1.7:0.7] plot({10^(\x)},\x) node[right]{$f(x)=\log_{10}{x}$};
\end{tikzpicture}
\caption{$f(x) = \log_n{x} $}
\label{fig:diagram25}
\end{figure}
最后,我们把对数函数和指数函数画到一个坐标系里。
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
%画x和y轴坐标
\draw[-Stealth] (-5.2,0)--(5.2,0); %-Stealth是一种箭头样式,在shapes.arrows中
\draw[-Stealth] (0,-5.2)--(0,5.2);
%画刻度
\foreach \x in {0,1,...,8}
{
\draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
%标坐标原点
\node[below] at (0.2,0){0};
%标x轴刻度值
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
% 标注y轴刻度
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
\draw[black,thick,domain=3.7:-2.2] plot(\x,{2^(-\x)}) node[left]{$f(x)=\frac{1}{2}^x$};
\draw[blue,thick,domain=-3.7:2.2] plot(\x,{2^(\x)}) node[right]{$f(x)=2^{x}$};
\draw[red,thick,domain=-2:1.5] plot(\x,{3^(\x)}) node[right]{$f(x)=3^{x}$} ;
\draw[yellow!70!blue,thick,domain=3.7:-2.2] plot({2^(-\x)},\x) node[right]{$f(x)=\log_{\frac{1}{2}}{x}$};
\draw[green!70!black,thick,domain=-3.7:2.2] plot({2^(\x)},\x) node[right]{$f(x)=\log_2{x}$};
\draw[red!70!yellow,thick,domain=-1.7:0.7] plot({10^(\x)},\x) node[right]{$f(x)=\log_{10}{x}$};
\end{tikzpicture}
\caption{$f(x)=\alpha ^x \ and \ f(x) = \log_n{x}$}
\label{fig:diagram26}
\end{figure}
\newpage
\subsection{三角函数}
先来看正弦函数
\[
f(x) = \sin {x}
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
% 绘制坐标轴
\draw[->] (-4,0) -- (4,0) node[right] {$x$};
\draw[->] (0,-4) -- (0,4) node[above] {$y$};
% 绘制网格
\foreach \x in {-3,-2,-1,1,2,3} {
\draw (\x,-0.1) -- (\x,0.1) node[anchor=north] {$\x$};
\draw (-0.1,\x) -- (0.1,\x) node[anchor=east] {$\x$};
}
% 可选:绘制原点
\fill (0,0) circle (2pt) node[below right] {$O$};
\draw[domain=-pi:pi]plot(\x,{sin(\x r)});
\node at(1.9,1.2){$f(x)=\sin x$};
\end{tikzpicture}
\caption{正弦函数}
\label{fig:diagram27}
\end{figure}
接下来是余弦函数
\[
f(x) = \cos {x}
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
% 绘制坐标轴
\draw[->] (-6,0) -- (6,0) node[right] {$x$};
\draw[->] (0,-6) -- (0,6) node[above] {$y$};
% 绘制网格
\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5} {
\draw (\x,-0.1) -- (\x,0.1) node[anchor=north] {$\x$};
\draw (-0.1,\x) -- (0.1,\x) node[anchor=east] {$\x$};
}
% 可选:绘制原点
\fill (0,0) circle (2pt) node[below right] {$O$};
\draw[blue, domain=-(3/2)*pi:(3/2)*pi]plot(\x,{cos(\x r)});
\node at(-1.9,1.2){$f(x)=\cos x$};
\end{tikzpicture}
\caption{余弦函数}
\label{fig:diagram28}
\end{figure}
\newpage
最后是正切函数
\[
f(x) = \tan {x}
\]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.6, samples=1000]
% 绘制坐标轴
\draw[->] (-6,0) -- (6,0) node[right] {$x$};
\draw[->] (0,-6) -- (0,6) node[above] {$y$};
% 绘制网格
\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5} {
\draw (\x,-0.1) -- (\x,0.1) node[anchor=north] {$\x$};
\draw (-0.1,\x) -- (0.1,\x) node[anchor=east] {$\x$};
}
% 可选:绘制原点
\fill (0,0) circle (2pt) node[below right] {$O$};
\draw[red,domain=-1.36:1.36]plot(\x,{tan(\x r)});
\draw[red,domain=1.78:4.5]plot(\x,{tan(\x r)});
\draw[red,domain=-4.5:-1.78]plot(\x,{tan(\x r)});
\node at(-4.5,4){$f(x)=\tan x$};
\end{tikzpicture}
\caption{正切函数}
\label{fig:diagram29}
\end{figure}
\end{document}
原文地址:https://blog.csdn.net/weixin_66946161/article/details/143841126
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!