Matplotlib基础入门--数据分析三大件完结

Python数据分析三大件基础入门已经跟新完毕

前言

  • Matplotlib 是python 的一个绘图库,提供了直方图、散点图……等图像的画法
  • 本文将介绍Matplotlib绘图的基本操作,想要熟练使用需要不断实战

Matplotlib基础步骤

import matplotlib.pyplot as plt
#设置字体
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"]

"""
1、创建画布
    plt.figure(figsize=(), dpi=)
    figsize: 指定长度
    dpi: 图像的清晰度
    返回flg对象

2、绘制图像
    以折线图为例
    plt.plot(x, y)
3、显示图像
    plt.show()
"""

例子:折线图绘制与显示

#1、创建画布
plt.figure(figsize=(10,10), dpi=100)
#2、绘制折线图
plt.plot([1,2,3,4,5,6,7,8], [1,2,3,4,5,6,7,8])
#3、显示图像
plt.show()


在这里插入图片描述

基础绘图功能

折线图

案例:画出城市11点到12点1小时内每分钟的温度折线图,范围:15~18度

1.1、准备数据并画出初始折线图

import matplotlib.pyplot as plt
import random

#数据
x = range(50)
y_shanghai = [random.uniform(15,18) for i in x]  #生成50个 15~18中间的数字
print(y_shanghai)
#1、创建画布
plt.figure(figsize=(20,8),dpi=80)

#2、绘制折线图
plt.plot(x, y_shanghai)

#3、显示图像
plt.show()
y_shanghai:随机数显示
[16.875817833161456, 15.451008116552151, 17.58067075895153, 17.288801706428366, 15.200932586506742, 16.909753257480574, 16.480645292862373, 17.42294691433753, 16.05638787307683, 15.474114004023923, 15.728478813481193, 17.832324581551454, 15.76244866525853, 16.72710315835741, 16.26421137400754, 17.727771939033705, 17.12830280854327, 15.803368544334655, 17.46081769393601, 17.97550957499392, 17.31322639092533, 15.46695162014346, 16.76494328781358, 15.786037685150003, 16.67695939202865, 17.984011492175945, 16.57571973389307, 17.290330688708703, 17.194655619506378, 17.997727061048515, 15.472782111662703, 16.387287685179523, 15.5249822661221, 17.405731776976634, 17.647349574547782, 15.38360790790629, 16.503231567098897, 16.699982734626243, 17.00959412214185, 17.002731294604832, 16.741321181338556, 16.10071468001527, 15.846096283640717, 17.222802062622463, 16.072327302102522, 16.57900024740193, 15.259950695978013, 16.74506786820688, 16.06341403220067, 17.815735953522566]

在这里插入图片描述

1.2、添加自定义刻度

1、构建x刻度和标签
x_ticks_label = [“11点{}分”.format(i) for i in x]
构建y刻度
y_ticks = range(40)

2、修改
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])

#构建x刻度和标签
x_ticks_label = ["11点{}分".format(i) for i in x]
#构建y刻度
y_ticks = range(40)

#修改
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])

1.3、中文乱码

#设置字体
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"]

1.4、加网格

plt.grid(True, linestyle='--', alpha=0.5)

1.5、添加描述信息

plt.xlabel("时间")
plt.ylabel("温度")
plt.title("11:00~12:00之间温度变化", fontsize=20)

1.6、保存图像

plt.savefig("test1.png")
#注意:plt.show()会释放figure资源,所以必须在显示前保存,显示后保存也只能保存空照片

1.7、总代码

import matplotlib.pyplot as plt
import random
from pylab import mpl

#设置中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

#0.准备数据
x = range(50)
y_shanghai = [random.uniform(15,18) for i in x]      #列表推导式

#1、创建画布
plt.figure(figsize=(20,8), dpi=100)

#2、绘制图像
plt.plot(x, y_shanghai)

#2.1、添加刻度
#添加标签
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(50)

#刻度显示
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])

#2.2、添加网格
plt.grid(True, linestyle="--", alpha=0.5)

#2.3、添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11~12点间温度变化", fontsize=20)

#2.4图像保存
plt.savefig("./test.png")

#3、图像显示
plt.show()


在这里插入图片描述

2、一个坐标显示多个图像

图像可以设置风格、颜色---->现用现查

案例

#0.准备数据
x = range(60) 
y_shanghai = [random.uniform(15,18) for i in x]    #随机数的使用
y_beijing = [random.uniform(1,3) for i in x]

#1.创建画布
plt.figure(figsize=(20, 8), dpi=100)

#2. 绘制图像
plt.plot(x, y_shanghai, label="上海")
plt.plot(x, y_beijing, label="北京", color="r", linestyle="--")

#2.1 添加x、y标签
#构造x,y刻度标签
x_ticks = ["11点{}分".format(i) for i in x]
y_ticks = range(60)

#刻度显示
plt.xticks(x[::5], x_ticks[::5])
plt.yticks(y_ticks[::5])

#2.2 添加网格
plt.grid(True, linestyle="--", alpha=0.5)

#2.3 添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11点到12点温度变化", fontsize=20)

#2.4 保存图像
plt.savefig("./test2.png")

#2.5 添加图例         有时候显示不出来label需要设置图例
plt.legend(loc=0)

#3. 图像显示
plt.show()


在这里插入图片描述

3、多个坐标显示

可以通过subplots函数实现(旧的版本中有subplot,使⽤起来不⽅便),推荐subplots函数
matplotlib.pyplot.subplots(nrows=1, ncols=1, **fig_kw) 创建⼀个带有多个axes(坐标系/绘图区)的图
解释:
nrows, ncols: 设置有几行几列数据
fig: 图对象
axes:返回相应数量的坐标系

设置标题等方式不同:
set_xticks
set_yticks
set_xlabel
set_ylabel

plt.函数名() 相当于面向过程,axes相当于面向对象画图方法

#0 准备数据
x = range(60)
y_shanghai = [random.uniform(5,10) for i in x]
y_beijing = [random.uniform(1,5) for i in x]

#1 创建画布
flg, axes = plt.subplots(nrows=1,ncols=2, figsize=(20,80),dpi=100)   #创建一个一行两列的子图

# 2 绘制图像
axes[0].plot(x, y_shanghai, label="上海")
axes[1].plot(x, y_beijing, color="r", linestyle="--", label="北京")

#2.1 添加x,y刻度
# 构造x,y轴刻度标签
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(10)

# 刻度显示
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_label[::5])   #刻度与标签分离

axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_label[::5])

# 添加网格
axes[0].grid(True, linestyle="--", alpha=0.5)
axes[1].grid(True, linestyle="--", alpha=0.5)

#添加描述信息
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[0].set_title("11点到12点温度变化图", fontsize=20)
axes[1].set_xlabel("时间")
axes[1].set_ylabel("温度")
axes[1].set_title("11点到12点温度变化图", fontsize=20)

# 图像保存
plt.savefig("./test3.png")

# 添加图例
axes[0].legend(loc=0)
axes[1].legend(loc=0)

#3 图像显示
plt.show()


在这里插入图片描述
6)

4、plot其他图像绘制

import numpy as np

#0 准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

#1 创建画布
plt.figure(figsize=(20,8), dpi=100)

#2 绘制函数图像
plt.plot(x, y)

#3 显示
plt.show()


在这里插入图片描述

5、其他

现用现查即可

#散点图
plt.scatter()
#柱状图
plt.bar()
#直方图
plt.his()
#饼图
plt.pie
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值