个性化阅读
专注于IT技术分析

使用Tikz的方程的Latex图

点击下载

本文概述

图形是通过链接连接的数据的图形表示。可以通过tikz环境创建图形, 也可以不使用tikz环境创建图形。但是, 通过使用tikz环境, 带有方程的图形会更好。

在本主题中, 一些示例包含方程式, 有些示例仅使用顶点。

让我们考虑一个绘制温度和电阻图的示例。

每个步骤将包括对相应命令的说明。该命令的描述将从%开始。

绘制图形的代码如下:

\documentclass[border=4.6pt]{standalone}% the standalone package is used to overcome the other preambles used in the sub-files.
% the length mentioned above is the length of the border of the whole graph page from the axis.
\usepackage{pgfplots} % the package used to implement the graphs
\pgfplotsset{width = 6.4cm, compat = newest} % it is the width of the graph. The lesser the width, the higher the graph will shrink.
\usepackage{sansmath} % it is the package used in math. It provides the math font.
\pgfplotsset{
% pgfplotsset is used to define the parameters used further to draw a graphs
  tick label style = { font=\tiny\sansmath\sffamily}, , % it defines the font size of the label
  label style = { font=\small\sansmath\sffamily}
}
\begin{document}   
\begin{tikzpicture}  % tikz environment commonly used
  \begin{axis} [
      domain = 0:100, xlabel     = Temperature, % You can label the axis according to your graph.
      ylabel     = Resistance, axis lines = left, % if you  use the right axis line, then the axis will be inverted
      clip       = true, xmin = 0, xmax = 100, ymin = 0, ymax = 100, xtick={38, 80}, % xtick and ytick are used to determine the range of the drawn graph. The labels will be adjusted according to the xtick and ytick only.
      xticklabels={$40$, $70$}, ytick={30, 80}, yticklabels={$30$, $70$}, ]
   \addplot [smooth, dashed] % dashed option draws the dashed curve. You can also use dotted, dashdotted, dashdotdotted.
      coordinates { (10, 5) (25, 10) (35, 20) (38, 22) (45, 26) (52, 29)
                    (61, 33) (72, 40) (79, 50) (80, 59)};
    \addplot [smooth]  % the smooth option is used to illustrate the smooth curve
      coordinates { (20, 22) (24, 25) (30, 29) (36, 31) (45, 36) (50, 40)
                    (60, 50) (65, 59) (69, 69) (70, 77)};
    % you can include as many curves in your graph according to the requirements.

\end{axis}
\end{tikzpicture}
\end{document}

输出:

使用Tikz的方程的Latex图

图轴的确定

常用的轴有两种。第一个是简单图, 其中包含四个象限, 另一个是轴, 用于绘制条形图等。

要绘制x和y轴, 必须声明一些包, 集合和库。这些将在下面给出的代码中列出。

让我们考虑第一个示例来绘制四个象限的轴。

\documentclass[12pt]{article} 
\usepackage{tikz} % package used for the tikz
\usepackage{pgfplots} % package used to draw plots
\usepgfplotslibrary{polar} % the polar library is used to implement the negative and positive axis
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$x$}, ylabel={$y$}, axis equal }} % used to declare my style command.
% you can also align the axis to the left, right, etc. according to the requirements. But the middle would produce the axis with four quadrants, and the left or right would result in only one quadrant.
\begin{document}
\begin{tikzpicture} % tikz environment 

\begin{axis}
[my style, xtick={-4, -3, -2, ..., 4}, ytick={-4, -3, -2, ..., 4}, % you can also expand the points or range of the x-axis and the y-axis.
xmin=-4, xmax=4, ymin=-4, ymax=4]
\end{axis}
\end{tikzpicture}

\end{document}

输出:

使用Tikz的方程的Latex图

两个示例之间只有一个区别, 即确定样式的类型。下面给出了绘制仅适用于一个象限的条形图等轴的代码:

\documentclass[10pt]{article} 
\usepackage{tikz} 
\usepackage{pgfplots}
\usepgfplotslibrary{polar} 
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{my style/.append style={axis x line=left, axis y line=
left, xlabel={$x$}, ylabel={$y$}, axis equal }} %  the middle would produce the axis with four quadrants, and the left or right would result in only one quadrant.
\begin{document}
\begin{tikzpicture} % tikz environment 

\begin{axis}
[my style, xtick={-2, -1, ..., 2}, ytick={-2, -1, ..., 2}, % you can also expand the points or the range of the x-axis and the y-axis.
xmin=-2, xmax=2, ymin=-2, ymax=2]
\end{axis}
\end{tikzpicture}

\end{document}

你需要在我的样式类别中使用向左或向右(用于反转轴), 而不是使用中间(对于四个象限)选项。你会注意到变化。

输出:

使用Tikz的方程的Latex图

注意:-my样式中指定的数字之间的点数(..)xtick = {-2, -1, …, 2}, ytick = {-2, -1, …, 2 }-命令应该相等。否则, Latex将考虑错误。

方程图

你也可以使用方程式绘制图形。包, 集合和库将与用于其他类型图的相同。

  • 一阶方程

该示例是仅使用一个阶方程来绘制图形。

公式如下:

使用Tikz的方程的Latex图

下面给出了绘制此类方程式的代码:

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usepackage{pgfplots}
\usepgfplotslibrary{polar} % to determine the polar coordinates
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$x$}, ylabel={$y$}, axis equal }} %  the middle would produce the axis with four quadrants, and the left or right would result in only one quadrant.
\begin{document}

\begin{tikzpicture}
\begin{axis}[my style]
\addplot[domain=-6:1] {-x+3}; % the domain signifies the range for the graph
\addplot[domain=1:6] {x};
\end{axis}
\end{tikzpicture}

\end{document}

输出:

使用Tikz的方程的Latex图
  • 二阶方程

你也可以绘制二阶方程的图形。代码和命令将类似于上述图形类型。

让我们绘制y = x2的图

此类图形的代码如下:

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usepackage{pgfplots}
\usepgfplotslibrary{polar} % to determine the polar coordinates
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$x$}, ylabel={$y$}, axis equal }} %  the middle would produce the axis with four quadrants, and the left or right would result in only one quadrant.
\begin{document}

\begin{tikzpicture}
\begin{axis}[my style]
\addplot {x^2}; % here, the second-order equation is written. To draw a graph for any equation, you need to mention the equation in this section. If you want to specify the domain, then you can include the domain option.

\end{axis}
\end{tikzpicture}

\end{document}

输出:

使用Tikz的方程的Latex图

同样, 你可以使用到目前为止讨论的方法在Latex中绘制任何形状和图形。

极坐标曲线

乳胶还提供了绘制极曲线所包围的区域的功能。

在这里, 代码将类似于上述方法。

让我们绘制曲线所包围的区域, r2 = 2 sin?θ

在这种情况下, 需要进行一些更改。第一个是包括样本。样本用于绘制平滑图。它用于复杂图形(如极坐标图)中。

你需要在代码中包含sample = 100。

另一种是指定域。取决于用户根据图形要求确定域的范围。

要绘制极坐标曲线, 你需要使用极坐标环境而不是如下所示的轴环境:

\begin{polaraxis}
............
\end{polaraxis}

方程r2 = 2 sin?θ现在变为, r =√2sin?θ

用于绘制极坐标曲线的轴与通常的轴不同。

要实现极曲线, 必须声明以下序言:

\pgfplotsset{my polar style/.append style={xticklabels={, , $\frac{\pi}{6}$, $\frac{\pi}{3}$, $\frac{\pi}{2}$, $\frac{2\pi}{3}$, $\frac{5\pi}{6}$, $\pi$, $\frac{7\pi}{6}$, $\frac{4\pi}{3}$, $\frac{3\pi}{2}$, $\frac{5\pi}{3}$, $\frac{11\pi}{6}$, }, thin }}

以上所有参数都是用于确定极坐标曲线的图形的刻度标签。

下面给出绘制极坐标图的代码:

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usepackage{pgfplots}
\usepgfplotslibrary{polar} % to determine the polar coordinates
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{my polar style/.append style={xticklabels={, , $\frac{\pi}{6}$, $\frac{\pi}{3}$, $\frac{\pi}{2}$, $\frac{2\pi}{3}$, $\frac{5\pi}{6}$, $\pi$, $\frac{7\pi}{6}$, $\frac{4\pi}{3}$, $\frac{3\pi}{2}$, $\frac{5\pi}{3}$, $\frac{11\pi}{6}$, }, thin }} % you can also mention thick instead of thin.
\begin{document}

\begin{tikzpicture}
\begin{polaraxis}[my polar style ] % the name of style depends on your choice. But make sure that the name used above in the append style should be same as the name used here.
\addplot[domain=0:90, samples=50]{sqrt(2*(sin(x))};
\addplot[domain=90:180, samples=50]{sqrt(2*(sin(x)))}; % you can modify the domain according to the requirements. The polar curve will be adjusted according to the specified domain or range. You can also change the sample size accordingly.
\end{polaraxis}
\end{tikzpicture}


\end{document}

输出:

使用Tikz的方程的Latex图

赞(0)
未经允许不得转载:srcmini » 使用Tikz的方程的Latex图

相关推荐

评论 抢沙发

评论前必须登录!