pgfplots¶
借助宏包pgfplots,可以使用TikZ绘制折线图,如下所示。
pgfplots绘制折线图
\begin{figure}[htbp]
\begin{tikzpicture}
\begin{axis}[sharp plot]
\addplot coordinates{(0, 5)(1, 9)(2, 7)(3, 4)(4, 10)};
\end{axis}
\end{tikzpicture}
\end{figure}
axis控制一个图像,\begin{axis}后可以加一对方括号,用于设定图片的参数。addplot控制图像中的一个序列,其后同样可以加一对方括号用于控制参数。
axis环境¶
axis环境可用的参数如下所列:
[sharp plot, smooth]等,表明折线图的曲线是否经过平滑xlabel与ylabel,控制坐标轴的标注。title控制图像的标题xmode与ymode控制坐标轴是线性坐标轴(normal)还是对数坐标轴(log)xtick distance与ytick distance控制坐标轴刻度大小xmin, xmax与ymin, ymax控制坐标轴的范围- 当指定坐标轴范围时,如果
enlargelimits指定为true,则坐标轴会自动放大以符合输入数据
- 当指定坐标轴范围时,如果
如下创建一个空的图像,x坐标轴为线性,y坐标轴为对数:
axis参数示例
\begin{figure}[htbp]
\begin{tikzpicture}
\begin{axis}[
xlabel=Year,
ylabel=Inventory,
xmin=2019, xmax=2021,
ymin=100, ymax=1000,
xtick distance=1,
title=Inventory Graph,
xmode=normal, ymode=log
]
\end{axis}
\end{tikzpicture}
\end{figure}
有关更多参数,请参见pgfplots 手册
\addplot¶
\addplot命令可以向图像中添加折线图或函数。
\addplot coordinates{}用于绘制折线图,其中coordinates中的坐标用圆括号括起\addplot [smooth] {f(x)}用于绘制平滑的折线图,其中f(x)为函数表达式
axis参数示例
\begin{figure}[htbp]
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$ variable,
ylabel=$y$ variable,
xmin=0, xmax=7,
title=Regression Example,
]
\addplot coordinates{(0, 5)(1, 9)(2, 7)(3, 4)(4, 10)};
\addlegendentry{\small Data}
\addplot [smooth, red]{0.5*x + 6};
\addlegendentry{\small Regressor}
\end{axis}
\end{tikzpicture}
\end{figure}
\addplot有如下参数:
smooth如果存在,绘制不包含点坐标的平滑图像[color]设置颜色。