笔记:Matplotlib的使用

使用Matplotlib库之前需要先进行安装,下面介绍两种方式

第一种方式

在Terminal下输入pip install matplot
等待一会就会安装好了,如果网速比较慢,可以使用镜像,比较常用的国内镜像包括:
(1)豆瓣 http://pypi.douban.com/simple/
(2)阿里云 http://mirrors.aliyun.com/pypi/simple/
(3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
使用示例方法如下
pip install matplot -i http://mirrors.aliyun.com/pypi/simple/

第二种方式

按照如下路径:File->Setting->Project:项目名(一般是倒数第四个)
->Project Interpreter->点击右侧加号->搜索matplot->点击右下角Install Package
1.
在这里插入图片描述2.
在这里插入图片描述3.
在这里插入图片描述4.
在这里插入图片描述5.
在这里插入图片描述按照这两种方式都可以安装matplot,下面用几个示例来讲解如何使用

画一个简单的图

import matplotlib.pyplot as plt
'''
画一个简单的图
'''
# 创建一块画布
plt.figure()
# 在折线图中画三个点(1,4)(0,5)(9,6)
plt.plot([1, 0, 9], [4, 5, 6])
# 显示
plt.show()

画一个折线图

import matplotlib.pyplot as plt
'''
设置画布属性与图片保存
'''
# 设置画布属性 figsize:画布大小;dpi:图像清晰度
plt.figure(figsize=(20, 8),dpi=80)  # 宽20,高8
# 折线图
plt.plot([1,2,3,4,5,6,7],[17,17,18,15,19,14,18])
# 保存图像;必须在show之前使用,因为show函数会释放资源
plt.savefig("test01.png")
# 显示
plt.show()

设置x,y轴刻度;显示网格;添加标签

import matplotlib.pyplot as plt
import random
'''
画出某城市12点到1点内每分钟湿度变化折线图
'''
# 准备数据
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]  # 创建60个15-18之间的数
# 创建画布
plt.figure(figsize=(20,8),dpi=80)
# 绘制图像
plt.plot(x, y_shanghai)
# 准备x轴的刻度说明
x_label = ["12点{}分".format(i) for i in x]
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 修改x,y轴刻度
plt.xticks(x[::5], x_label[::5])  # 第一个值为刻度的点,第二个值为刻度显示的值
plt.yticks(range(0,40,5))
# 显示网格
plt.grid(True, linestyle="--", alpha=0.5)  # 虚线,透明度为0.5
# 添加描述信息
plt.xlabel("时间变化")
plt.ylabel("湿度变化")
plt.title("12点到1点内每分钟湿度变化折线图")
# 4.显示
plt.show()

在一个折线图中画两个折线

import matplotlib.pyplot as plt
import random
# 准备数据
x = range(60)
y_shanghai = [random.uniform(16,19) for i in x]
y_shenyang = [random.uniform(1,4) for i in x]
# 创建画布
plt.figure(figsize=(20,8),dpi=80)
# 绘制图像
plt.plot(x, y_shanghai, color="r", linestyle="--", label="上海")  # 红色,虚线
plt.plot(x, y_shenyang, color="b", label="沈阳")  # 蓝色,实线
# 显示图例
plt.legend()
# 准备x的刻度说明
x_label = ["12点{}分".format(i) for i in x]
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 修改x,y轴刻度
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0,40,5))
# 显示网格
plt.grid(True, linestyle="--", alpha=0.5)  # 虚线,透明度为0.5
# 添加描述信息
plt.xlabel("时间变化")
plt.ylabel("湿度变化")
plt.title("11点到12点内每分钟湿度变化折线图")
# 4.显示
plt.show()

在一个画布中画两个折线图

import matplotlib.pyplot as plt
import random
# 准备数据
x = range(60)
y_shanghai = [random.uniform(16,19) for i in x]
y_shenyang = [random.uniform(1,4) for i in x]
# 创建画布
# nrows:几行;ncols:几列;figure:画布;axes:绘图区
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
# 绘制图像
axes[0].plot(x, y_shanghai, color="r", linestyle="--", label="上海")  # 红色,虚线
axes[1].plot(x, y_shenyang, color="b", label="沈阳")
# 显示图例
axes[0].legend()
axes[1].legend()
# 准备x的刻度说明
x_label = ["12点{}分".format(i) for i in x]
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 修改x,y轴刻度
axes[0].set_xticks(x[::5])  # 画x轴刻度
axes[0].set_xticklabels(x_label[::5])  # 画x轴刻度值
axes[0].set_yticks(range(0,40,5))  # 画y轴刻度值
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_label[::5])
axes[1].set_yticks(range(0,40,5))
# 显示网格
axes[0].grid(True, linestyle="--", alpha=0.5)  # 虚线,透明度为0.5
axes[1].grid(True, linestyle="--", alpha=0.5)  # 虚线,透明度为0.5
# 添加描述信息
axes[0].set_xlabel("时间变化")
axes[0].set_ylabel("湿度变化")
axes[0].set_title("12点到1点内每分钟湿度变化折线图")
axes[1].set_xlabel("时间变化")
axes[1].set_ylabel("湿度变化")
axes[1].set_title("12点到1点内每分钟湿度变化折线图")
# 4.显示
plt.show()

散点图

import numpy as np
import matplotlib.pyplot as plt
'''
散点图
'''
x = [115.15, 151.48, 132.15, 157.16, 324.15, 159.45, 789.16, 157.89]
y = [123.45, 456.12, 123.15, 163.15, 154/45, 136.78, 789.45, 467.16]
plt.figure(figsize=(20, 8), dpi=80)
plt.scatter(x, y)
plt.show()

柱状图


import matplotlib.pyplot as plt
'''
柱状图
'''
# 1、准备数据
movie_names = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴', '降魔传','追捕','七十七天','密战','狂兽','其它']
tickets = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]

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

# 3、绘制柱状图
x_ticks = range(len(movie_names))
plt.bar(x_ticks, tickets, color=['b','r','g','y','c','m','y','k','c','g','b'])
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 修改x刻度
plt.xticks(x_ticks, movie_names)

# 添加标题
plt.title("电影票房收入对比")

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

# 显示图像
plt.show()

多个柱状图

import matplotlib.pyplot as plt
# 1、准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']

first_day = [10587.6,10062.5,1275.7]  # 首日票房
first_weekend=[36224.9,34479.6,11830]  # 首周票房

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

# 3、绘制柱状图
plt.bar(range(3), first_day, width=0.2, label="首日票房")
plt.bar([0.2, 1.2, 2.2], first_weekend, width=0.2, label="首周票房")  # 为了不覆盖掉上面的刻度,统一向右平移0.2
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 显示图例
plt.legend()

# 修改刻度
plt.xticks([0.1, 1.1, 2.1], movie_name)

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

直方图

import matplotlib.pyplot as plt

'''
直方图:直方图描述的是一组数据的频次分布,以矩阵的长度表示每一组的频数,宽度表示组距
组数:在统计数据时,我们把数据按照不同的范围分成几组,分成的组的个数成为组数
组距:每一组两个端点的差
api:matplotlib.pyplot.hist(x, bins=None, density=True, **kwargs) bins是组数,x是数据,density=True:显示y轴的频数
组数的公式:极差/组距=(max-min)/组距
'''
# 设置数据
time = [131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
# 布置画布
plt.figure(figsize=(20, 8), dpi=80)
# 组距
distance = 2
# 组数
group_num = int((max(time) - min(time)) / distance)
# 显示直方图
plt.hist(time, bins=group_num)
# 修改x轴刻度
plt.xticks(range(min(time), max(time), distance))
# 添加网格
plt.grid(linestyle="--", alpha=0.5)
# 显示图像
plt.show()

饼图

import matplotlib.pyplot as plt

'''
饼图:类别太多的时候不建议使用饼图
api:plt.ple(x, labels=,autopct=,colors)
x:数量,自动计算百分比
labels:每部分名称
autopct:占比显示指定 %1.2f%%
colors:颜色
'''
# 1、准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']

place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]

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

# 3、绘制饼图
plt.pie(place_count, labels=movie_name, colors=['b','r','g','y','c','m','y','k','c','g','y'], autopct="%1.2f%%")
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei']  # 黑体
# 显示图例
plt.legend()
# 防止图片变形
plt.axis('equal')

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值