1.Matplotlib简介
Matplotlib是一个Python的2D绘图库,它可以在各种平台上以各种硬拷贝格式和交互环境生成具有出版品质的图形。通过Matplotlib,开发者可以仅需要几行代码,便可以生成绘图、直方图、功率谱、条形图、错误图、散点图等。
以下是Matplotlib的一些关键特点:
-
易于使用的图形绘制接口:Matplotlib试图让简单的事情变得更简单,让无法实现的事情变得可能实现。你可以通过简单的函数调用绘制出高质量的图形。
-
适用于多种操作系统和图形后端:Matplotlib支持各种操作系统上多种图形后端,这意味着它可以与各种环境和应用程序无缝集成。
-
丰富的输出格式:Matplotlib支持多种输出格式,包括PNG, PDF, SVG, EPS等,这些格式非常适合于在文档和网页中使用。
-
可定制性:Matplotlib具有极高的可定制性,你可以控制图形的大小、分辨率、颜色和样式等。
-
强大的社区支持:由于Matplotlib是开源项目,因此它拥有一个活跃的开发者社区,不断地在改进和增强其功能。
2.Matplotlib图形相关属性
这里的Figure相当于一张白纸,Axes为一个个图

一个例子,在这个Figure中有一个两行两列的图

画图的两种方式
第一种方式为pyplot API做图
第二种方式为自身面向对象的方式

简单的做一个折线图
python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.plot([1,3,5],[2,4,6])
plt.show()

python
#添加x轴信息
plt.xlabel('X轴')
#添加y轴信息
plt.ylabel('Y轴')
这里的异常是由于我们当前使用的字体是不支持汉字的,因此我们需要调整一下字体

python
#查看matplotlib支持的字体
import matplotlib
[f.name for f in matplotlib.font_manager.fontManager.ttflist]
选择一个支持汉字的即可,我们这里选择宋体再运行

python
plt.rcParams['font.sans-serif'] = "Songti SC"

python
# 添加标题
plt.title('标题')

python
# 设置x轴和y轴间距
plt.xticks([1,2,3,4,5])
plt.yticks([2,3,4,5,6])

在这个axes内在画一条折线,并且给折线添加标注信息
python
plt.plot([1,3,5],[2,4,6],label='first') #添加第一条折线,标注为 first
plt.plot([1,3,5],[6,6,6],label='second') #添加第二条折线,标注为 second
plt.title('标题')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.xticks([1,2,3,4,5])
plt.yticks([2,3,4,5,6])
plt.legend() # 显示折线的标注,在右下方
plt.show()

给线条设置颜色,图线样式,以及标记
python
plt.plot([1,3,5],[2,4,6],label='first',color='g',linestyle='-.',marker='*')

其中

3.子图的布局
子图就是在一个figure图像中会有多个axes;其中每个子图用行列以及第几个(从1开始)作为坐标
定位一个子图的方法,就是找到子图对应的下标

在这种情况下有三个子图;
第一个子图占了两个位置,确定第一个子图的坐标要站在第一个子图角度去看待,那么也就是有1行,两列,所以它的下标为(1,2,1)
第二个子图,站在第二个子图角度看一共有两行两列,所以它的下标为(2,2,2)
第三个子图(2,2,4),因为站在第三个子图角度依然是有两行两列,其中index为4,因为站在这个角度,1和3合并了
所以理解子图位置一定要站在自身角度看待整个全局

【自己理解分析】,一般情况下是分布对称的

4.创建子图
add_subplot添加子图
add_subplot(self, *args, **kwargs)[source]
在 Matplotlib 中,add_subplot
函数是用于在图表中添加子图的。这个函数通常与 plt.subplot
函数一起使用,后者用于创建一个新的子图并将其添加到当前的图表中。
参数:
-
*args
: 三个整数参数,分别代表子图在rows
、cols
和num
位置。例如,plt.subplot(2, 2, 1)
将添加一个位于 2x2 子图网格中的第一个子图。 -
**kwargs
: 关键字参数,用于设置子图的属性,例如plt.subplot(1, 1, 1, frameon=False)
将创建一个没有边框的子图。
返回值:
add_subplot
函数返回一个 Axes
对象,该对象代表子图。通过这个对象,您可以进一步设置子图的属性,例如标题、坐标轴标签、网格等。
【示例】
python
# 使用oop方式
fig = plt.figure();
# 创建第一个子图
ax1 = fig.add_subplot(2,2,1)
# 创建第二个子图
ax2 = fig.add_subplot(2,2,2)
# 创建第三个子图
ax3 = fig.add_subplot(2,2,3)
# 创建第四个子图
ax4 = fig.add_subplot(2,2,4)

python
# 给子图添加标题
# 注意这里是oop方式,使用set_title
ax1.set_title('first')

python
# 使用pyplot 添加子图
ax1 = plt.subplot(2,2,1)
ax2 = plt.subplot(2,2,2)
ax3 = plt.subplot(2,2,3)
ax4 = plt.subplot(2,2,4)
ax1.set_title('first') # 给第一个子图添加标题
plt.title('test')# 默认添加给最新的子图标题

addsubplots添加子图
python
# 使用oop 方式添加子图
fig = plt.figure()
axes = fig.subplots(2,3) # 创建两行三列子图 返回的axes是一个数组
#通过数组下标,可以对对应的子图进行操作
axes[0][0].set_title('first')
axes[0,2].set_title('third')
python
# 使用pyplot api添加子图
fig,axes = plt.subplots(2,3) # 返回的对象是figure 和 axes
axes[0,0].set_title('first')
axes[1,2].set_title('last')
x = [1,2,3,4,5]
y = [x**2 for x in x]
axes[1,2].plot(x,y) # 对最后的一个子图进行绘制
plt.show()

总结
绘制子图有两种方式,第一种是使用面向对象API,第二种是使用pyplotAPI
使用add_subplots是添加一个绘制一个
使用subplots是事先知道要绘制几个子图
使用函数的时候一定要分区别是哪个方式和使用哪个函数,