Python -- Matplotlib:设置画布大小和子图个数

  • 只有一个子图时

    plt.figure()                           #默认画布大小
    plt.figure( figsize=(width,height) )   #自定义画布大小(width,height)
    plt.plot(...)                          #使用plt绘图
    
  • 有多个子图时(但在一张画布上)

    • 方法1:使用add_subplot
    # 用 2x2 个子图为例
    fig = plt.figure( [figsize=(width,height)] )   #定义整个画布
    ax1 = fig.add_subplot(221)                     #第一个子图
    ax1.plot(...)                                  #在子图上作图
    ax2 = fig.add_subplot(222)
    ax2.plot(...) 
    ax3 = fig.add_subplot(223)
    ax3.plot(...) 
    ax4 = fig.add_subplot(224)
    ax4.plot(...)               
    
    • 方法2:使用subplots
    # 仍用 2x2 个子图为例
    fig,axes = plt.subplots( 2,2, [figsize=(width,height)] )  
    ax = axes.flatten()   
    ax[0].plot(...)   #若不flatten axes,则这里用axes[0,0]
    ax[1].plot(...) 
    ...             
    

更新的内容

  • 调整子图间距

    plt.tight_layout()
    
  • 18
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值