matplotlib中绘制多个子图形

本文详细介绍了在matplotlib库中如何使用subplot()函数创建多个子图形,并通过subplots_adjust()函数调整子图间的间距。内容包括subplot()函数的参数用法以及subplots_adjust()函数的wspace和hspace参数设置,最后通过实例展示了具体操作和输出结果。
摘要由CSDN通过智能技术生成
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()

输出结果为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世澈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值