使用Jupyter来进行Matplotlib的绘制
- Jupyter notebook使用
进入虚拟环境
workon ai
# 输入命令
jupyter notebook
1.实现一个简单的Matplotlib图形。
import matplotlib as plt
%matplotlib inline
plt.figure() # 创建一个画布
plt.plot([1,3,4],[5,6,8])
plt.show()
2.Matplotlib三层结构
1 容器层
容器层主要由Canvas、Figure、Axes组成。
Canvas是位于最底层的系统层,在绘图的过程中充当画板的角色,即放置画布(Figure)的工具。
Figure是Canvas上方的第一层,也是需要用户来操作的应用层的第一层,在绘图的过程中充当画布的角色。
Axes是应用层的第二层,在绘图的过程中相当于画布上的绘图区的角色。
Figure:指整个图形(可以通过plt.figure()设置画布的大小和分辨率等)
Axes(坐标系):数据的绘图区域
Axis(坐标轴):坐标系中的一条轴,包含大小限制、刻度和刻度标签
特点为:
一个figure(画布)可以包含多个axes(坐标系/绘图区),但是一个axes只能属于一个figure。
一个axes(坐标系/绘图区)可以包含多个axis(坐标轴),包含两个即为2d坐标系,3个即为3d坐标系
2 辅助显示层
辅助显示层为Axes(绘图区)内的除了根据数据绘制出的图像以外的内容,主要包括Axes外观(facecolor)、边框线(spines)、坐标轴(axis)、坐标轴名称(axis label)、坐标轴刻度(tick)、坐标轴刻度标签(tick label)、网格线(grid)、图例(legend)、标题(title)等内容。
该层的设置可使图像显示更加直观更加容易被用户理解,但又不会对图像产生实质的影响。
3 图像层
图像层指Axes内通过plot、scatter、bar、histogram、pie等函数根据数据绘制出的图像。
总结:
Canvas(画板)位于最底层,用户一般接触不到
Figure(画布)建立在Canvas之上
Axes(绘图区)建立在Figure之上
坐标轴(axis)、图例(legend)等辅助显示层以及图像层都是建立在Axes之上
3.折线图绘制与保存图片
import matplotlib.pyplot as plt
plt.figure() # 创建画布(容器层)
# 绘制折线图(图像层)
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17, 17, 18, 15, 11, 11, 13])
# 展示图片
plt.show()
4 设置画布属性与图片保存
import matplotlib.pyplot as plt
# 创建画布(容器层)
# figsize:画布的大小
# dpi:图像的清晰度
plt.figure(figsize=(20, 8), dpi=100)
# 绘制折线图(图像层)
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17, 17, 18, 15, 11, 11, 13])
plt.show
# 保存图片到指定位置
plt.savefig("text.png")
案例:显示温度变化状况
需求:画出某城市11点到12点1小时内每分钟的温度变化折线图,温度范围在15度~18度
效果:
1 准备数据并画出初始折线图
import matplotlib.pyplot as plt
import random
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制折线图
plt.plot(x, y_shanghai)
# 显示图像
plt.show()
2 添加自定义x,y刻度
import matplotlib.pyplot as plt
import random
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/System/Library/Fonts/PingFang.ttc')
x = range(60)
y_shanghai=[random.uniform(15,18) for i in x]
# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制折线图
plt.plot(x, y_shanghai)
# 构建x,y刻度标签
x_ticks_label=["11点{}分".format(i) for i in x]
y_ticks=range(40)
# 修改x,y轴坐标的刻度显示
plt.xticks(x[::5], x_ticks_label[::5],fontproperties=font)
plt.yticks(y_ticks[::5])
# 显示图像
plt.show()
3 添加网格显示
plt.grid(True, linestyle='--', alpha=0.5)
4添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11点0分到12点之间的温度变化图示")
设置图形风格
颜色字符 | 风格字符 |
---|---|
r 红色 | - 实线 |
g 绿色 | - - 虚线 |
b 蓝色 | -. 点划线 |
w 白色 | : 点虚线 |
c 青色 | ’ ’ 留空、空格 |
m 洋红 | |
y 黄色 | |
k 黑色 |
完整代码
# 画出温度变化图
import random
# 准备x, y坐标的数据
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=80)
# 2)绘制折线图
# plt.plot(x, y_shanghai)
plt.plot(x, y_shanghai, label="上海")
# 使用多次plot可以画多个折线
plt.plot(x, y_beijing, color='r', linestyle='--', label="北京")
# 显示图例
plt.legend(loc="best")
# 增加以下两行代码
# 构造x轴刻度标签
x_ticks_label = ["11点{}分".format(i) for i in x]
# 构造y轴刻度
y_ticks = range(40)
# 修改x,y轴坐标的刻度显示
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])
# 添加网格显示
plt.grid(True, linestyle='--', alpha=0.5)
# 添加x轴、y轴描述信息及标题
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11点0分到12点之间的温度变化图示")
# 3)显示图像
plt.show()
北京上海显示在两个画布上
# 画出温度变化图
import random
# 准备x, y坐标的数据
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=80)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)
# 2)绘制折线图
# plt.plot(x, y_shanghai)
# plt.plot(x, y_shanghai, label="上海")
axes[0].plot(x, y_shanghai, label="上海")
# 使用多次plot可以画多个折线
# plt.plot(x, y_beijing, color='r', linestyle='--', label="北京")
axes[1].plot(x, y_beijing, color='r', linestyle='--', label="北京")
# 显示图例
# plt.legend(loc="best")
axes[0].legend()
axes[1].legend()
# 增加以下两行代码
# 构造x轴刻度标签
x_ticks_label = ["11点{}分".format(i) for i in x]
# 构造y轴刻度
y_ticks = range(40)
# 修改x,y轴坐标的刻度显示
# plt.xticks(x[::5], x_ticks_label[::5])
# plt.yticks(y_ticks[::5])
axes[0].set_xticks(x[::5], x_ticks_label[::5])
axes[0].set_yticks(y_ticks[::5])
axes[1].set_xticks(x[::5], x_ticks_label[::5])
axes[1].set_yticks(y_ticks[::5])
# 添加网格显示
# plt.grid(True, linestyle='--', alpha=0.5)
axes[0].grid(True, linestyle='--', alpha=0.5)
axes[1].grid(True, linestyle='--', alpha=0.5)
# 添加x轴、y轴描述信息及标题
# plt.xlabel("时间")
# plt.ylabel("温度")
# plt.title("中午11点0分到12点之间的温度变化图示")
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[0].set_title("上海中午11点0分到12点之间的温度变化图示")
axes[1].set_xlabel("时间")
axes[1].set_ylabel("温度")
axes[1].set_title("北京中午11点0分到12点之间的温度变化图示")
# 3)显示图像
plt.show()
画函数图像
import numpy as np
# 1)准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
# 2)创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 3)绘制函数图像
plt.plot(x, y)
# 添加网格显示
plt.grid()
# 4)显示图像
plt.show()
画散点图
- 探究不同变量之间的内在关系
x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64,
163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51,
21.61, 483.21, 245.25, 399.25, 343.35]
y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34,
140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 ,
30.74, 400.02, 205.35, 330.64, 283.45]
# 创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 绘制散点图
plt.scatter(x, y)
# 显示图像
plt.show()
比较相同天数的票房
movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]
x=range(len(movie_name))
# 创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 绘制柱状图
plt.bar(x, first_day, width=0.2, label="首日票房")
plt.bar([i+0.2 for i in x], first_weekend, width=0.2, label="首周票房")
# 显示图例
plt.legend()
# 修改x轴刻度显示
plt.xticks([i+0.1 for i in x], movie_name)
# 显示图像
plt.show()
直方图
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=100)
# 绘制直方图
# 设置组距
distance = 2
# 计算组数
group_num = int((max(time) - min(time)) / distance)
# 绘制直方图
plt.hist(time, bins=group_num)
# 修改x轴刻度显示
plt.grid(linestyle="--", alpha=0.5)
# 添加x, y轴描述信息
plt.xlabel("电影时长大小")
plt.ylabel("电影的数据量")
# 显示图像
plt.show()
饼图
# 1)准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
# 2)创建画布
plt.figure(figsize=(20, 8), dpi=100)
# 3)绘制饼图
plt.pie(place_count, labels=movie_name, autopct="%1.2f%%", colors=['b','r','g','y','c','m','y','k','c','g','y'])
# 显示图例
plt.legend()
# 添加标题
plt.title("电影排片占比")
# 让饼图显示圆形
plt.axis('equal')
# 4)显示图像
plt.show()