当前位置:网站首页>数据分析(一)——matplotlib
数据分析(一)——matplotlib
2022-07-31 22:14:00 【flag:卷王!】
什么是数据分析?
数据分析是用适当的方法对收集来的大量数据进行分析,帮助人们做出判断,以便采取适当行动。
数据分析的流程:

matplotlib :
- 能将数据进行可视化,更直观的呈现
- 使数据更加客观、更具说服力
matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建。
一、matplotlib折线图
示例:
from matplotlib import pyplot as plt
x=range(2,26,2)
#数据在X轴的位置,是一个可迭代对象
y=[15,13,14,5,17,20,35,26,26,27,22,18]
#数据在Y轴的位置,也是一个可迭代对象
#绘图
plt.plot(x,y) #传入X,Y形成对应坐标,通过plot绘制出折线图
#展示图形
plt.show()结果:
但是根据图可以发现一些问题:
- 描述信息,比如X轴与Y轴表示什么,这个图表示什么
- 调整刻度的间距
- 线条的样式(比如颜色,透明度等)
- 标记特殊点(比如最低点、最高点)
- 添加水印(防止被盗用)
改良(1)------图片大小与刻度
from matplotlib import pyplot as plt
fig=plt.figure(figsize=(20,8),dpi=80)
#figure图形图标的意思,在这里指的是我们的图,通过figure传递参数
#在图像模糊可以传入dpi参数,让图片清晰
x=range(2,26,2)
y=[15,13,14,5,17,20,35,26,26,27,22,18]
#绘图
plt.plot(x,y)
#设置x、y轴的刻度
plt.xticks(range(2,26,2))
plt.yticks(range(min(y),max(y)+1,2))
plt.savefig("./sig_size.png") #保存图片,保存为svg这种矢量图格式,放大不会有锯齿
#展示图形
plt.show()结果:

示例:
如果列表表示10点到12点的每一分钟的气温,如何绘制折线图观察每分钟气温的变化情况?
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf")
x=range(0,120)
y=[random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度
_xtick_labels=["10点{}分".format(i) for i in range(60)]
_xtick_labels+=["11点{}分".format(i) for i in range(60)]
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font) #rotation旋转的度数
plt.show()结果:

发现中文没有显示,因为matplotlib默认不支持中文字符
那么如何修改matplotlib的默认字体?
fc-list :lang=zh -->查看支持中文(冒号前面有个空格)
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x=range(0,120)
y=[random.randint(20,35) for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度
_xtick_labels=["10点{}分".format(i) for i in range(60)]
_xtick_labels+=["11点{}分".format(i) for i in range(60)]
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45) #rotation旋转的度数
plt.show()结果:
改良(2)---中文显示(字体+大小)、绘制网格、添加图例以及多条绘图
from matplotlib import pyplot as plt
import matplotlib
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf",size=25) #中文显示以及字体大小
fig=plt.figure(figsize=(20,8),dpi=80)
#figure图形图标的意思,在这里指的是我们的图,通过figure传递参数
#在图像模糊可以传入dpi参数,让图片清晰
x=range(2,26,2)
y_1=[15,13,14,5,17,20,35,26,26,27,22,18]
y_2=[12,15,12,15,16,20,21,23,19,30,12,17]
#绘图(多条)
plt.plot(x,y_1,label="今天",linestyle='--') #linestyle决定线的形况
plt.plot(x,y_2,label="昨天",linestyle='-.')
#设置x、y轴的刻度
plt.xticks(range(2,26,2))
plt.yticks(range(min(y_1),max(y_1)+1,2),fontproperties=my_font)
plt.yticks(range(min(y_2),max(y_2)+1,2),fontproperties=my_font)
#添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度 单位(℃)",fontproperties=my_font)
plt.title("一天内温度变化",fontproperties=my_font)
plt.savefig("./sig_size.png") #保存图片,保存为svg这种矢量图格式,放大不会有锯齿
#绘制网格
plt.grid(alpha=0.4) #alpha设置透明度
#添加图例
plt.legend(prop=my_font,loc="upper right") #注意此时中文显示用prop,loc来决定图例的方向
#展示图形
plt.show()结果:

相关线的性质:

二、matplotlib常用统计图
1)绘制散点图
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf",size=15)
y_1=[11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_2=[26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]
x_1=range(1,32)
x_2=range(51,82)
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
#使用scatter方法绘制散点图,和之前绘制折线图的唯一区别
plt.scatter(x_1,y_1,label="3月份")
plt.scatter(x_2,y_2,label="10月份")
#调整x轴刻度
_x=list(x_1)+list(x_2)
_xtick_labels=["3月{}日".format(i) for i in x_1]
_xtick_labels+=["10月{}日".format(i-50) for i in x_2]
plt.xticks(_x[::3],_xtick_labels[::3],fontproperties=my_font,rotation=45)
#添加图例
plt.legend(loc="upper right",prop=my_font)
#添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)
#展示
plt.show()
结果:

2)绘制条形图
#纵向
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf",size=12)
a=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5","摔跤吧!爸爸","加勒比海盗5","金刚","极限特工","生化危机6","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3","蜘蛛侠","悟空传","银河护卫队2","情圣","新木乃伊"]
b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
#设置图形大小
plt.figure(figsize=(20,10),dpi=80)
#绘制条形图
plt.bar(range(len(a)),b,width=0.3)
#设置字符串到x轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)
plt.xlabel("影片",fontproperties=my_font)
plt.ylabel("票房(亿元)",fontproperties=my_font)
plt.savefig("./movie.png")
#展示
plt.show()
#横向
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf",size=12)
a=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5","摔跤吧!爸爸","加勒比海盗5","金刚","极限特工","生化危机6","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3","蜘蛛侠","悟空传","银河护卫队2","情圣","新木乃伊"]
b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
#设置图形大小
plt.figure(figsize=(20,10),dpi=80)
#绘制条形图
plt.barh(range(len(a)),b,height=0.3,color="red")
#设置字符串到y轴
plt.yticks(range(len(a)),a,fontproperties=my_font)
plt.ylabel("影片",fontproperties=my_font)
plt.xlabel("票房(亿元)",fontproperties=my_font)
plt.savefig("./movie.png")
#展示
plt.show()结果:


3)绘制直方图
from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf",size=12)
a=[random.randint(70,121) for i in range(250)]
#计算组数
d=2 #组距
num_bins =(max(a)-min(a))//d
#设置图形的大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins,density=True)
#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))
plt.grid(alpha=0.3)
#展示
plt.show()结果:

边栏推荐
- sqlite3 simple operation
- 嵌入式开发没有激情了,正常吗?
- Go mode tidy reports an error go warning “all” matched no packages
- In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
- 架构实战营模块八作业
- 求n以内的素数
- Shell script quick start to actual combat -02
- BM5 合并k个已排序的链表
- C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
- BOW/DOM (top)
猜你喜欢

Count characters in UTF-8 string function

PCB stackup design

The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days

财务盈利、偿债能力指标

Structure of the actual combat battalion module eight operations

Flex layout in detail

老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11

Embedded development has no passion, is it normal?

Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...

Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
随机推荐
BOW/DOM (top)
【论文精读】iNeRF
嵌入式开发没有激情了,正常吗?
【愚公系列】2022年07月 Go教学课程 025-递归函数
The article you worked so hard to write may not be your original
Returns a zero-length array or empty collection, do not return null
Douyin fetches video list based on keywords API
GateWay implements load balancing
Istio introduction
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
架构实战营模块八作业
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点
基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)
[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array
How to get useragent
useragent online lookup
支付模块实现
Arduino框架下STM32全系列开发固件安装指南
MATLAB program design and application 2.4 Common internal functions of MATLAB
Realize serial port receiving data based on STM32 ring queue