基础环境
\documentclass[tikz]{stanalone} %
\usepackage{pgfplots} % 包导入
\begin{document}
\begin{tikzpicture} % 画板
\begin{axis} % 坐标
content...
\end{axis}
\end{tikzpicture}
\end{document}
后续均默认次模板,书写范围从
axis
算起\begin{axis} content... \end{axis}
有特殊说明会进行提示。
一般图像
折线图
\begin{axis}[sharp plot] % sharp plot: 折线图,通过修改此类型,即可完成多种图形绘制
\addplot coordinates{
(0,1)
(1,3)
(2,4)
(3,1)
(4,5)
(5,2)
};
% addplot: 添加绘图
% coordinates: 点坐标,无需分隔符
\end{axis}
光滑曲线
\begin{axis}[smooth] % smooth: 光滑曲线
\addplot coordinates{
(0,1)
(1,3)
(2,4)
(3,1)
(4,5)
(5,2)
};
\end{axis}
条形图
\begin{axis}[ybar,enlargelimits=0.25] % ybar: 条形图, enlargelimits: 柱子之间最大间隔
% 可以控制图像属性
\addplot[draw=yellow, fill=orange] coordinates{
(0,1)
(1,3)
(2,4)
(3,1)
(4,5)
(5,2)
};
% 可以进行多个图像同意坐标系的绘制
\addplot[draw=blue, fill=green] coordinates{
(0,1)
(1,3)
(2,4)
(3,1)
(4,5)
(5,2)
};
\end{axis}
同坐标会自动编排。
直方图
\begin{axis}[ybar interval] % ybar interval: 连续柱形图?好像式直方图的养足
\addplot[fill=red,hist={bins=9}]% hist设置分组为9
table[row sep=\\,y index=0] { % row sep: 数据行分隔符, y index: 从0开始计
data\\ % data: 单行数据名称,内部自动映射
1\\
2\\2\\
3\\3\\3\\
4\\4\\4\\4\\
5\\5\\5\\5\\5\\
6\\6\\6\\6\\
7\\7\\7\\
8\\8\\
9\\
};
\end{axis}
函数图像
二维
\begin{axis}[domain=-10:10,axis x line=center, axis y line=center]
% domain: 定义域
% axis x line: x轴
% axis y line: y轴
\addplot {x}; % y = x
\addplot {1.5^x }; % y = 1.5 ^ x
\end{axis}
三维
\begin{axis}[colorbar] % colorbar: 显示彩色指示条
\addplot3[surf] % 三维绘图
{x^2 + y^2}; % 函数式
\end{axis}
补充
\begin{axis}[domain=-pi:pi, title=this is title, hide axis,legend pos=outer north east]
% domain: 定义域
% title: 标题
% hide axis:隐藏坐标系
% legend pos: 图例位置
\addplot {sin(deg(x))}; % deg: 转弧度
\addlegendentry{$f(x) = sin(x)$} % 图例:f(x) = sin(x)
\addplot {cos(deg(x))}; %
\addlegendentry{$f(x) = cos(x)$}; % 图例:f(x) = cos(x)
\end{axis}
帽子
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer} % 颜色
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-8:8, % 定义域
title=hat, % 标题
hide axis, % 隐藏坐标
colormap/cool, % 引入的颜色
]
\addplot3[mesh, samples=50]{ % 网格
% $f(x, y) = \frac{\sqrt{x^2 + y^2}}{\sqrt{x^2 + y^2}}$
sin(deg(sqrt(x^2+y^2))) / sqrt(x^2+y^2)};
% legend
\addlegendentry{$f(x)=\frac{sin(r)}{r} \quad r = \sqrt{x^2 + y^2}$ },;
\end{axis}
\end{tikzpicture}
\end{document}