【LaTex】3.5 数据分析


  在数据分析岗,需要做大量数据可视化的图标,LaTex在这方面可不弱于其他软件。PgfPlots就是一个专做数据可视化的LaTex包,我就简单写写常见的数据可视化图表。当然,pgfplots还可以用来绘制二维及三维函数图形。

axis参数

  如果用LaTex进行数据分析中的数据可视化绘图,那么柱状图、条形图和折线图是需要用axis指令来做的,对于axis指令,必须熟练掌握关键的五个参数。
  1, title参数,是配置图的标题;
  2, symbolic x coordssymbolic y coords参数,是配置x坐标轴或y坐标轴为文字,如果不配置的话,就是数字了,因为默认的坐标轴是数轴嘛。
  3. legend,本意是传奇,但是这里的意思不是这个。当一个图表里有多组数据时,就组成了一个legend,意思是图例,legend为每组数据提供一个名字。
  4. xtickytick,首先得搞懂tick是啥意思。翻字典是无用的,因为真实意思不在字典里,tick在这里的意思是刻度。我们经常看到这样的配置ytick=data,啥意思呢?就是以数据为y轴刻度。再比如xtick distance=50,就是x轴刻度间距为50。
  5. axis x lineaxis y line,这两个配置也是经常看到的,意思是什么呢?因为默认的坐标轴是一个方框,但是我们想画的图只有左和下两个坐标轴,所以需要用这两个配置只保留左边的y轴和下面的x轴。

柱状图

  以下是柱状图的代码:

\documentclass[UTF8]{article} 
\usepackage{pgfplots}
\usepackage{ctex}

\pgfplotsset{compat=1.16} 
\begin{document} 
	\begin{tikzpicture}
		\begin{axis}
			[ybar,
			 title={我的支出},
			 symbolic x coords={202201, 202202, 202203, 202204},
			 legend style={at={(0.55,0.8)}},
			 axis y line=left,
			 axis x line=bottom,
			 nodes near coords,
			 enlarge x limits=0.2, ] 
			\addplot+ coordinates {(202201, 1420) (202202, 1460) (202203, 1370) (202204, 1480)}; 
			\addplot+ coordinates {(202201, 321) (202202, 412) (202203, 279) (202204, 345)}; 
			\addplot+ coordinates {(202201, 1600) (202202, 1560) (202203, 1490) (202204, 1615)}; 
			\legend{吃饭, 交通, 娱乐}; 
		\end{axis} 
	\end{tikzpicture} 
\end{document}

  编译后效果:
在这里插入图片描述

条形图

  条形图是横向的柱状图。从柱状图,我们知道axis有个重要的属性是ybar,这个ybar决定了绘制出的图像是柱状图,那是不是也有xbar呢?确实是这样,xbar就是条形图,也就是横向的柱状图。以下是我写的条形图代码:

\documentclass[UTF8]{article} 
\usepackage{pgfplots}
\usepackage{ctex}

\pgfplotsset{compat=1.16} 
\begin{document} 
	\begin{tikzpicture}
		\begin{axis}
			[xbar,	
			title=考试成绩表,
			symbolic y coords={老张, 老刘, 老王, 老谢},
			ytick=data,
			axis y line=left,
			axis x line=bottom,
			nodes near coords,nodes near coords align={horizontal}, ] 
			\addplot coordinates {(67,老张) (85,老刘) (91,老王) (72,老谢)}; 
		\end{axis} 
	\end{tikzpicture}
\end{document}

  编译效果如下:
在这里插入图片描述
  程序员们,喜欢在逗号后面加上空格。但是要注意,TIKZ的坐标里面逗号后面是不允许有空的,假使我写了一个空格,如“(85, 老刘)”,编译后就会报这个错误:

Package pgfplots Error: Sorry, the input coordinate ` 老刘' has not been debeen misspelled? Or did you mean something like [normalized] 老刘?. ...张) (85, 老刘) (91,老王) (72,老谢)};

  这个细节是一定要注意的,加空格是好习惯,但是在TikZ里坐标里的空格是语法错误!

饼图

  饼图使用专门的包就可以了,以下是代码:

\documentclass[UTF8]{article} 
\usepackage{pgfplots}
\usepackage{ctex}
\usepackage{pgf-pie}  
\pgfplotsset{compat=1.16} 
\begin{document} 
	我的消费
	
\begin{tikzpicture}
	
	\pie{22.97/吃饭,
		22.97/娱乐,
		8.11/交通,
		8.11/衣服,
		6.76/日用化学品,
		31.07/房租}
	
\end{tikzpicture}
\end{document}

  显示效果:
在这里插入图片描述

折线图

  将柱状图加一个smooth,就成了折线图。下面是一个简单的例子:

\documentclass[UTF8]{article} 
\usepackage{pgfplots}
\usepackage{ctex}
\usepackage{pgf-pie}  
\pgfplotsset{compat=1.16} 
\begin{document} 
	\begin{tikzpicture}
		\begin{axis}[
			ybar,
			xlabel=月份,
			ylabel=王者荣耀充值,
			width=10cm,height=7cm,
			legend style={at={(0.0,.91)},anchor=west},
			nodes near coords
			]
			
			% Add values and attributes for the first plot
			\addplot[smooth,color=red,mark=x] coordinates {
				(1, 70)
				(2, 80)
				(3, 90)
				(4, 100)
				(5, 110)
				(6, 120)
			};
			
			% Add values and attributes for the second plot
			\addplot[smooth,color=blue,mark=*] coordinates {
				(1, 90)
				(2, 100)
				(3, 110)
				(4, 120)
				(5, 130)
				(6, 140)
			};
			
			\legend{英雄,皮肤}
		\end{axis}
	\end{tikzpicture}
\end{document}

  效果如图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醒过来摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值