python入门学习笔记11(subplot)

CODE1:

import matplotlib.pyplot as plt

plt.figure()
plt.subplot(2,2,1)
plt.plot([0,1],[0,1])

plt.subplot(2,2,2)
plt.plot([0,1],[0,2])

plt.subplot(2,2,3)
plt.plot([0,1],[0,3])

plt.subplot(2,2,4)
plt.plot([0,1],[0,4])

plt.show()

RESULT:
在这里插入图片描述

CODE2:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# method 1:subplot2grid
#########################
plt.figure()
ax1=plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)    #(3,3)整个grid三行三列,ax1起始(0,0),列跨度3,行跨度1
ax1.plot([1,2],[1,2])
ax1.set_title('ax1_title')
ax2=plt.subplot2grid((3,3),(1,0),colspan=2,)
ax3=plt.subplot2grid((3,3),(1,2),rowspan=2)
ax4=plt.subplot2grid((3,3),(2,0))
ax5=plt.subplot2grid((3,3),(2,1))

#method 2:gridspec
#########################
plt.figure()
gs=gridspec.GridSpec(3,3)    # 三行三列
ax1=plt.subplot(gs[0,:])    # 第一行全占 
ax1.set_title('ax1')
ax2=plt.subplot(gs[1,:2])    #第2行,占0,1两列
ax2.set_title('ax2')
ax3=plt.subplot(gs[1:,2])   #第1,2行占,第2列
ax3.set_title('ax3')
ax4=plt.subplot(gs[-1,0])    # 负数表示倒数第几,-1表示倒数第1个
ax4.set_title('ax4')
ax5=plt.subplot(gs[-1,-2])    # 表示最后1行第2列
ax5.set_title('ax5')

# method 3:easy to define structure
###############################################
f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[1,2])

plt.tight_layout()
plt.show()

RESULT:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值