Python数据可视化之使用GridSpec自定义子图

使用了很长时间的python,每次画图时都会忘记很多画图的方式,这里整理一下怎么在一张画布上定义多张子图。

引用模块:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec

生成几个随机点:

random_cp = np.random.random_integers(1, 100, (100, 2))
random_nums = np.random.random_integers(1, 100, 100)
random_cp1 = np.random.random_integers(1, 100, (100, 2))
random_nums1 = np.random.random_integers(1, 100, 100)

1、使用add_subplot

通过add_subplot就可以实现子图的划分:

先是通过plt.figure()创建画布,之后再通过add_subplot()在画布上创建不同的区域。

其中add_subplot()的参数,类似221就是将画布分为2行2列的也就是2×2的区域,使用第1个区域。

fig1 = plt.figure()

ax1 = fig1.add_subplot(221)
ax2 = fig1.add_subplot(222)

ax3 = fig1.add_subplot(223)
ax4 = fig1.add_subplot(224)

ax1.plot(range(len(random_nums)), random_nums)
ax3.scatter(random_cp[:, 0], random_cp[:, 1])
ax4.plot(range(len(random_nums1)), random_nums1)
ax2.scatter(random_cp1[:, 0], random_cp1[:, 1])

plt.show()


2、使用GridSpec

先是通过gridspec.GridSpec()创建区域,参数5,5的意思就是每行五个,每列五个,最后就是一个5×5的画布,相比于add_subplot(),使用网格布局的话可以更加灵活的控制占用多少空间。

gs = gridspec.GridSpec(5,5)
fig1 = plt.figure()
ax1 = fig1.add_subplot(gs[0:2, 0:2])
ax2 = fig1.add_subplot(gs[3:5, 0:2])

ax3 = fig1.add_subplot(gs[0:2, 3:5])
ax4 = fig1.add_subplot(gs[3:5, 3:5])

ax1.plot(range(len(random_nums)), random_nums)
ax3.scatter(random_cp[:, 0], random_cp[:, 1])
ax4.plot(range(len(random_nums1)), random_nums1)
ax2.scatter(random_cp1[:, 0], random_cp1[:, 1])

ax1.plot(range(len(random_nums)), random_nums)
ax3.scatter(random_cp[:, 0], random_cp[:, 1])
ax4.plot(range(len(random_nums1)), random_nums1)
ax2.scatter(random_cp1[:, 0], random_cp1[:, 1])

plt.show()



  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值