latex-pgfplots基础绘图

基础环境

\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}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值