当前位置:网站首页>matplotlib数据可视化
matplotlib数据可视化
2022-07-28 05:22:00 【Alan and fish】
- 导入绘图库
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
- 读取数据(数据来源是一个EXCLE表格,这里演示的是如何将数据可视化出来)
os.chdir(r'E:\jupyter\数据挖掘\数据与代码')
df = pd.read_csv('air_data.csv',na_values= '--')
1.折线图
plt.plot(x,y,ls=,lw=,c=,marker=,markersize=,markeredgecolor=,markerfacecolor, label=)
x: x轴上的数值
y: y轴上的数值
ls- -函数线条风格(=‘-’ 实线, ‘–’ 虚线 ,‘-.’ 点划线 ,‘:’ 实点线)
lw: 线条宽度
c: 颜色
marker: 线条上点的形状, 常用为’o’,即圆点形状
markersize: 线条上点的形状
markeredgecolor: 点的边框色
markerfacecolor: 点的填充色
# 绘制观察窗口内的飞行次数和观测窗口内的总飞行里程数
# 支持中文显示
plt.rcParams['font.sans-serif']=['SimHei']# 字体
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(0,10,100)
y=np.sin(x)
plt.plot(x,y,ls='-',lw=2,marker='o',markersize=5,c='red',markeredgecolor='black',markerfacecolor='lightskyblue')
plt.show()

2.直方图
- hist:数据
- bins:组距
- color:填充色
- edgecolor:边框色
- density:是否绘制成概率密度形式
- xlabel:横坐标
- ylabel:纵坐标
- labelpad/pad:离坐标轴的距离
# 绘制年龄的分布情况
plt.hist(x=df['AGE'],bins=30,color='r',edgecolor='black',density=True) # density=True 代表是否绘制概率密度形式
plt.xlabel('客户年龄',fontsize=15,labelpad=20)
plt.ylabel('频数',fontsize=15,labelpad=20)
plt.title('年龄分布图',fontsize=15,pad=20)
plt.show()

3.箱线图
plt.boxplot(x,notch,sym,vert,whis,position,widths,patch_artist,meanline,showmeans, boxprops,labels,flierprops)
x: 数据
宽度:宽度
patch_artist: 是否填充箱体颜色
meanline:是否显示均值
showmeans: 是否显示均值
meanprops;设置均值属性,如点的大小,颜色等
medianprops:设置中位数的属性,如线的类型,大小等
showfliers: 是否表示有异常值
boxprops:设置箱体的属性,边框色和填充色
cappops: 设置箱线顶端和末端线条的属性,如颜色,粗细等
age=df[df['AGE'].notnull()]['AGE'] # 剔除年龄的空值
plt.boxplot(x=age,patch_artist=True,boxprops={
'color':'red'})
plt.show()

4.柱状图
# 将字符型数据转换date格式
df['FFP_DATE']=pd.to_datetime(df['FFP_DATE'],format='%Y/%m/%d',errors='coerce') # errors 避免报错
data=df['FFP_DATE'].dt.year.value_counts()
x_data=data.index
y_data=data.values
plt.bar(x=x_data,height=y_data,align='center',color='y',tick_label=x_data)
plt.title('不同年份的会员数量',pad=5)
plt.show()

5.饼图
- autopct:设置百分比的格式
data=df['GENDER'].value_counts()
# 绘制饼图
plt.pie(x=data.values,labels=data.index,colors=['lightskyblue','lightcoral'],autopct='%.1f%%')
plt.show()

6.散点图
# 飞行次数与总飞行公里数的关系
plt.scatter(x=df['FLIGHT_COUNT'],y=df['SEG_KM_SUM'],color='steelblue',marker='o',s=100)
plt.title('飞行次数与总飞行公里数的关系')
plt.show()

边栏推荐
- svn incoming内容无法更新下来,且提交报错:svn: E155015: Aborting commit: XXX remains in conflict
- CertPathValidatorException:validity check failed
- XShell突然间无法连接虚拟机
- 小程序开发系统有哪些优点?为什么要选择它?
- DataX installation and use
- Structured streaming in spark
- JS promise implementation logic
- 发售预告:7月22日“大暑”发售,【传统国风廿四节气】夏季发售完毕。
- 4个角度教你选小程序开发工具?
- 【七】redis缓存与数据库数据一致性
猜你喜欢

记录下在线扩容服务器遇到的问题 NOCHANGE: partition 1 is size 419428319. it cannot be grown
Installation and use of sqoop

How digital library realizes Web3.0 social networking

Distributed cluster architecture scenario optimization solution: session sharing problem

Use Python to encapsulate a tool class that sends mail regularly

At the moment of the epidemic, online and offline travelers are trapped. Can the digital collection be released?

Mars数*字*藏*品*平*台守卫者计划细节公布

分布式集群架构场景优化解决方案:分布式调度问题

分布式锁-数据库实现

【5】 Redis master-slave synchronization and redis sentinel (sentinel)
随机推荐
变量,流程控制与游标
1:为什么数据库要分库分表
Invalid packaging for parent POM x, must be “pom“ but is “jar“ @
Distributed cluster architecture scenario optimization solution: distributed ID solution
【二】redis基础命令与使用场景
Flume installation and use
Notice of attack: [bean Bingbing] send, sell, cash, draw, prize, etc
【六】redis缓存策略
高端大气的小程序开发设计有哪些注意点?
XShell突然间无法连接虚拟机
Sqoop安装及使用
Use Python to encapsulate a tool class that sends mail regularly
Chapter IX sub query (key)
Two methods of covering duplicate records in tables in MySQL
MySQL multi table query
分布式集群架构场景优化解决方案:分布式ID解决方案
微信小程序开发语言一般有哪些?
速查表之各种编程语言小数|时间|base64等操作
Invalid packaging for parent POM x, must be “pom“ but is “jar“ @
数字藏品以虚强实,赋能实体经济发展