subplot()函数
- 语法结构
plt.sublot(a,b,c)
- 参数 a 表示子图有几行;
- 参数 b 表示子图有几列;
- 参数 c 表示当前子图的序号;
subplots_adjust()函数
- 语法结构
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0.15,hspace=0.15)
- 参数取值范围为0~1之间的小数,表示百分比;
- wspace,hspace表示子图之间的举例;
举例
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import sklearn
matplotlib.rc("font",family='FangSong')
df = pd.read_csv('data\\boston.csv')
# 读取数据集
df = df.sample(500, replace=False, random_state=1, axis=0)
# 随机取其中500行数据,随机数种子设置为1
fig = plt.figure(figsize=(8, 8), dpi=100)
plt.subplots_adjust(hspace=0.35)
plt.subplot(2, 2, 1)
plt.scatter(df["CRIM"], df["MEDV"], label="CRIM-MEDV")
plt.xlabel("犯罪率")
plt.ylabel("房价")
plt.subplot(2, 2, 2)
plt.scatter(df["INDUS"], df["MEDV"], label="INDUS-MEDV")
plt.xlabel("商用土地比例")
plt.ylabel("房价")
plt.subplot(2, 2, 3)
plt.scatter(df["B"], df["MEDV"], label="B-MEDV")
plt.xlabel("黑人比例")
plt.ylabel("房价")
plt.subplot(2, 2, 4)
plt.scatter(df["LSTAT"], df["MEDV"], label="LSTAT-MEDV")
plt.xlabel("中低端住户比例")
plt.ylabel("房价")
plt.show()
输出结果为: