数据分析笔记Matplotlib(7)-Subplot 多图分隔显示

import matplotlib.pyplot as plt

plt.figure()


"""
创建第1个小图, (3,3)表示将整个图像窗口分成3行3列, (0,0)表示从第0行第0列开始作图,colspan=3表示此小图跨3列, rowspan=1表示行的跨度为1. colspan和rowspan默认跨度为1且为缺省值. 
"""
ax1 = plt.subplot2grid((3,3),(0,0),colspan=3)
ax1.plot([1,2], [1,2])    # 第一个小图两个点连线,x为1和1,y与之对应的为2和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))


ax4.scatter([1,2], [2,2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y')

plt.show()

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


plt.figure()
gs = gridspec.GridSpec(3, 3) #将窗口分为三行三列


ax6 = plt.subplot(gs[0, :])#第一个小图占据第一行的所有列
ax7 = plt.subplot(gs[1, :2])
ax8 = plt.subplot(gs[1:, 2])
ax9 = plt.subplot(gs[-1, 0])#-1代表倒数第一行
ax10 = plt.subplot(gs[-1, -2])

plt.show()

import matplotlib.pyplot as plt

plt.figure()


"""
建立一个2行2列的图像窗口,sharex=True表示共享x轴坐标, sharey=True表示共享y轴坐标. ((ax11, ax12), (ax13, ax14))表示第1行从左至右依次放ax11和ax12, 第2行从左至右依次放ax13和ax14. 
"""
f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)

ax11.scatter([1,2], [1,2])#散点图,两个点(1,1),(2,2)

plt.tight_layout() #图像紧凑
plt.show()

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值