matplotlib库(1)

相关概念和基本用法

import matplotlib.pyplot as plt

# matplotlib中的figure对象、axes对象、axis对象的区别:
# figure对象相当于一个画图板或者画布,是一个基础载体
# axes对象相当于画布上的多个区域的集合,axis对象相当于axes上的一个绘图区域

# 创建一个figure和一个axes,每个axes包含四个axis
figure1, axes1 = plt.subplots(2, 2)
# 访问第一个axis
axis1 = axes1[0, 0]
# 设置x轴标签
axis1.set_xlabel("x1")
# 创建另外一个figure和axes,每个axes包含六个axis
figure2, axes2 = plt.subplots(2, 3)
axis2 = axes2[0, 0]
axis2.set_xlabel("x2")

# subplots函数和subplot函数的区别:
# subplots函数每次创建一个新的figure和一个新的axes
# subplot函数则使用当前的figure,只创建一个新的axes,并指定其中的一个axis

# 创建一个figure和2×2的axes
figure, axes = plt.subplots(2, 2)
# 使用当前figure创建一个新的2×3的axes,并指定当前使用第一个axis
axis = plt.subplot(2, 3, 1)
axis.set_xlabel("x")

# 面向对象风格和plt风格
# 上面的代码使用的都是面向对象风格,优点是显示创建figure和axes,缺点是实际编程中无法在软件中快速调出相关函数
# 使用plt风格主要就是针对这种缺点,而且代码简单明了,但是隐藏了figure和axes、axis
# 创建第一个figure
plt.figure(1)
# 在第一个figure中创建一个2×2的axes,当前使用的是第一个axis
plt.subplot(2, 2, 1)
# 设置第一个axis的x轴标签
plt.xlabel("x1")
# 在第一个figure中创建一个2×3的axes,当前使用的是第二个axis
plt.subplot(2, 3, 2)
# 设置第二个axis的x轴标签
plt.xlabel("x2")
# 创建第二个figure
plt.figure(2)
plt.subplot(2, 2, 1)
plt.xlabel("x3")
plt.subplot(2, 2, 2)
plt.xlabel("x4")


plt.show()


猜你喜欢

转载自blog.csdn.net/weixin_49346755/article/details/121220533