本文主要是介绍Python Matplotlib基础,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Matplotlib
1.什么是Matplotlib?
-
mat - matrix plot - 画图 lib - library
-
专门用于开发2D图表的Python工具库
-
使用起来非常简单
-
以渐进、交互式方式实现数据可视化
2.Matplotlib三层结构
2.1 容器层
容器层主要由Canvas、Figure、Axes组成。
Canvas是位于最底层的系统层,在绘图过程中充当画板的角色,即防止画布(Figure)的工具
Figure是Canvas上方的第一层,也是需要用户操作的应用层的第一层,在绘图过程中充当画布的角色。
Axes是应用层的第二层,在绘图的过程中相当于画布上的绘图区的角色
- Figure:指整个图形(可以通过plt.figure()设置画布的大小和分辨率等)
- Axes(坐标系):数据的绘图区域
- Axis(坐标轴):坐标系中的一条轴,包含大小的限制、刻度和刻度标签
2.2 辅助显示层
辅助显示层为Axes(绘图区)内的除了根据数据绘制出的图像以外的内容,主要包括Axes外观(facecolor)、边框线(spines)、坐标轴(axis)、坐标轴名称(axis label)、坐标轴刻度(tick)、坐标轴刻度标签(tick label)、网格线(grid)、图例(legend)、标题(title)等内容。
该层的设置可使图像显示更加直观更加容易被用户理解,但又不会对图像产生实质的影响
2.3 图像层
图像层指Axes内通过plot、scatter、bar、histogram、pie等函数根据数据绘制出的图像。
3 实战案例
3.1简单案例 - 绘制折线图
import matplotlib.pyplot as plt
# 1.创建画布 figsize:画布大小 dpi:dot per inch 画像的清晰度
plt.figure(figsize=(20, 8), dpi=80)
# 2.绘制图像
plt.plot([1, 2, 3, 4, 5, 6, 7], [17, 17, 18, 15, 11, 12, 11])
# 3.保存图像(保存代码在show()方法之后会出现问题,保存为空白图片)
plt.savefig('test78.png')
# 4.显示图像(会释放所有的画布资源)
plt.show()
3.2 在同一个画布绘制两条线
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.数据准备
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]
# 2.创建画布 figsize:画布大小 dpi:dot per inch 画像的清晰度
plt.figure(figsize=(15, 8), dpi=200)# 3.绘制图像
# color : r:红色 g:绿色 b:蓝色 w:白色 c:青色 m:洋红 y:黄色 k:黑色
# linestyle : -实线 --虚线 -.点划线 :点虚线 ''留空,空格
# label :
plt.plot(x, y_shanghai, color='r', linestyle='--', label='ShangHai')
plt.plot(x, y_beijing, color='b', linestyle='-', label='BeiJing')# 显示图例
# loc:显示位置 best, upper right,upper left,lower right,lower left,right,center left...
plt.legend(loc='lower left')# 3.1修改x,y刻度
# 准备x的刻度说明(刻度说明应与刻度步长保持一致)
x_label = ["11:{}".format(i) for i in x]
# 修改刻度显示
plt.xticks(x[::5], x_label[::5])
plt.yticks(range(0, 40, 5))
# 3.2增加网格显示 linstyle:线条风格 alpha:透明度
plt.grid(True, linestyle='-', alpha=0.5)
# 3.3添加标题
plt.xlabel("time")
plt.ylabel("temperature")
plt.title("temperature of anyone city")# 4.保存图像(保存代码在show()方法之后会出现问题,保存为空白图片)
plt.savefig('test78.png')
# 5.显示图像(会释放所有的画布资源)
plt.show()
3.3 绘制多个绘图区
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.数据准备
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(1, 3) for i in x]
# 2.创建画布 figsize:画布大小 dpi:dot per inch 画像的清晰度
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5), dpi=100)# 3.绘制图像
# color : r:红色 g:绿色 b:蓝色 w:白色 c:青色 m:洋红 y:黄色 k:黑色
# linestyle : -实线 --虚线 -.点划线 :点虚线 ''留空,空格
# label : 图例
axes[0].plot(x, y_shanghai, color='r', linestyle='--', label='ShangHai')
axes[1].plot(x, y_beijing, color='b', linestyle='-', label='BeiJing')# 显示图例
# loc:显示位置 best, upper right,upper left,lower right,lower left,right,center left...
axes[0].legend(loc='upper left')
axes[1].legend(loc='upper left')# 3.1修改x,y刻度
# 准备x的刻度说明(刻度说明应与刻度步长保持一致)
x_label = ["11:{}".format(i) for i in x]
# 修改刻度显示
axes[0].set_xticks(x[::10])
axes[0].set_xticklabels(x_label[::10])
axes[0].set_yticks(range(0, 40, 5))
axes[1].set_xticks(x[::10])
axes[1].set_xticklabels(x_label[::10])
axes[1].set_yticks(range(0, 40, 5))
# 3.2增加网格显示 linstyle:线条风格 alpha:透明度
axes[0].grid(True, linestyle='-', alpha=0.5)
axes[1].grid(True, linestyle='-', alpha=0.5)
# 3.3添加标题
axes[0].set_xlabel("time")
axes[0].set_ylabel("temperature")
axes[0].set_title("temperature of anyone city")
axes[1].set_xlabel("time")
axes[1].set_ylabel("temperature")
axes[1].set_title("temperature of anyone city")# 4.保存图像(保存代码在show()方法之后会出现问题,保存为空白图片)
plt.savefig('test78.png')
# 5.显示图像(会释放所有的画布资源)
plt.show()
3.4 绘制数学图像
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.准备数据
x = np.linspace(-1, 1, 1000)
y = 2 * x * x
# 2.创建画布
plt.figure()
# 3.绘制图像
plt.plot(x, y)
# 4.显示图像
plt.show()
3.5 散点图绘制
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.准备数据
n = range(100)
x = [random.uniform(0, 10) for i in n]
y = [random.uniform(0, 10) for i in n]
# 2.创建画布
plt.figure(figsize=(10, 10), dpi=50)
# 3.绘制图像
plt.scatter(x,y)
# 4.显示图像
plt.show()
3.6 柱状图绘制
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.准备数据
x = ['The Lord of the Rings', 'Hobbit', 'The Chronicles of Narnia', 'Harry Potter', 'Alice\'s Adventures in Wonderland']
y = [1542, 1552, 6351, 2215, 1256]
# 2.创建画布
plt.figure(figsize=(10, 10), dpi=50)
# 3.绘制图像
plt.bar(range(len(x)), y, color=['b', 'r', 'm', 'y', 'c'])
# 修改刻度
plt.xticks(range(len(x)), x)
# 添加标题
plt.title("movie pay")
# 添加网格显示
plt.grid(linestyle='--', alpha=0.2, color='r')# 4.显示图像
plt.show()
3.7绘制多个柱状图在一个图内
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.准备数据
movie_name = ['The Lord of the Rings', 'Hobbit', 'The Chronicles of Narnia', 'Harry Potter','Alice\'s Adventures in Wonderland']
first_day = [1542, 1552, 6351, 2215, 1256]
first_weekend = [4562, 5123, 8521, 6362, 5215]# 2.创建画布
plt.figure(figsize=(10, 10), dpi=50)# 3.绘制图像
plt.bar([0, 1, 2, 3, 4], first_day, width=0.2, label='first day')
plt.bar([0.2, 1.2, 2.2, 3.2, 4.2], first_weekend, width=0.2, label='first weekend')
#显示图例
plt.legend()
# 修改刻度
plt.xticks([0.1, 1.1, 2.1, 3.1, 4.1], movie_name)
# 添加标题
plt.title("movie pay")
# 添加网格显示
plt.grid(linestyle='--', alpha=0.2, color='r')# 4.显示图像
plt.show()
3.8 绘制饼状图
import matplotlib.pyplot as plt
import random
import numpy as np
# 1.准备数据
movie_name = ['The Lord of the Rings', 'Hobbit', 'The Chronicles of Narnia', 'Harry Potter','Alice\'s Adventures in Wonderland']
place_count = [1542, 1552, 6351, 2215, 1256]
# 2.创建画布
plt.figure(figsize=(12, 6), dpi=80)
# 3.绘制图像
plt.pie(place_count, labels=movie_name, autopct='%1.2f%%')
plt.axis('equal')
# 显示图例
plt.legend(loc='upper right')
# 4.显示图像
plt.show()
这篇关于Python Matplotlib基础的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!