0
点赞
收藏
分享

微信扫一扫

数据分析课程笔记(一)简介、jupyter和conda、matplotlib

猎书客er 2022-02-03 阅读 43

数据分析课程笔记

数据分析简介

什么是数据分析
数据分析是用适当的方法对收集来的大量数据进行分析,帮助人们作出判断,以便采取适当行动

数据分析流程

在这里插入图片描述

jupyter和conda的使用

命令重名名

conda

在这里插入图片描述
下载地址:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
conda : data science package & environment manager
创建环境:
conda create --name python3 python=3
切换环境:
windows :activate python3
linux/macos : source activate python3

官方地址: https://www.anaconda.com/download/

jupyter notebook

jupyter notebook:一款编程/文档/笔记/展示软件

启动命令:jupyter notebook
在这里插入图片描述

在这里插入图片描述

matplotlib

在这里插入图片描述

1.能将数据进行可视化,更直观的呈现
2.使数据更加客观、更具说服力

什么是matplotlib

matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建

基本要点

在这里插入图片描述
axis轴,指的是x或者y这种坐标轴
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

设置图片大小

在这里插入图片描述
在这里插入图片描述

调整X或者Y轴上的刻度

在这里插入图片描述

# coding=utf-8
from matplotlib import pyplot as plt

x = range(2,26,2)
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]

#设置图片大小
plt.figure(figsize=(20,8),dpi=80)



#绘图
plt.plot(x,y)

#设置x轴的刻度
_xtick_labels = [i/2 for i in range(4,49)]
plt.xticks(range(25,50))
plt.yticks(range(min(y),max(y)+1))

#保存
# plt.savefig("./t1.png")

#展示图形
plt.show()

那么问题来了:
如果列表a表示10点到12点的每一分钟的气温,如何绘制折线图观察每分钟气温的变化情况?
a= [random.randint(20,35) for i in range(120)]
在这里插入图片描述
在这里插入图片描述

设置中文显示

为什么无法显示中文:
matplotlib默认不支持中文字符,因为默认的英文字体无法显示汉字
查看linux/mac下面支持的字体:
fc-list 查看支持的字体
fc-list :lang=zh 查看支持的中文(冒号前面有空格)

那么问题来了:如何修改matplotlib的默认字体?
通过matplotlib.rc可以修改,具体方法参见源码(windows/linux)
通过matplotlib 下的font_manager可以解决(windows/linux/mac)

在这里插入图片描述

# coding=utf-8
from matplotlib import pyplot as plt
import random
import matplotlib
from matplotlib import font_manager

#windws和linux设置字体的方法
# font = {'family' : 'MicroSoft YaHei',
#         'weight': 'bold',
#         'size': 'larger'}
# matplotlib.rc("font",**font)
# matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")

#另外一种设置字体的方式
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,fontproperties=my_font) #rotaion旋转的度数

#添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度 单位(℃)",fontproperties=my_font)
plt.title("10点到12点每分钟的气温变化情况",fontproperties=my_font)

plt.show()
/System/Library/Fonts/PingFang.ttc: 苹方\-简,蘋方\-簡,PingFang SC:style=中黑体,中黑體,Medium
/System/Library/Fonts/PingFang.ttc: 苹方\-港,蘋方\-港,PingFang HK:style=中黑体,中黑體,Medium
/System/Library/Fonts/PingFang.ttc: 苹方\-港,蘋方\-港,PingFang HK:style=纤细体,纖細體,Thin
/System/Library/Fonts/PingFang.ttc: 苹方\-简,蘋方\-簡,PingFang SC:style=纤细体,纖細體,Thin

折线图实例

在这里插入图片描述

# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
x = range(11,31)

#设置图形大小
plt.figure(figsize=(20,8),dpi=80)

plt.plot(x,y)

#设置x轴刻度
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)
plt.yticks(range(0,9))

#绘制网格 alpha透明度
plt.grid(alpha=0.1)

#展示
plt.show()

在这里插入图片描述

双折线图实例

在这里插入图片描述
在这里插入图片描述

# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 = [1,0,3,1,2,2,3,3,2,1 ,2,1,1,1,1,1,1,1,1,1]

x = range(11,31)

#设置图形大小
plt.figure(figsize=(20,8),dpi=80)

plt.plot(x,y_1,label="自己",color="#F08080")
plt.plot(x,y_2,label="同桌",color="#DB7093",linestyle="--")

#设置x轴刻度
_xtick_labels = ["{}岁".format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)
# plt.yticks(range(0,9))

#绘制网格
plt.grid(alpha=0.4,linestyle=':')

#添加图例
plt.legend(prop=my_font,loc="upper left")

#展示
plt.show()

在这里插入图片描述
matplotlib 官网实例
https://matplotlib.org/stable/gallery/index
在这里插入图片描述

常用统计图

在这里插入图片描述

在这里插入图片描述

举报

相关推荐

0 条评论