Matplotlib 不同大小的子图 [英] Matplotlib different size subplots

查看:70
本文介绍了Matplotlib 不同大小的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图形中添加两个子图.一个子图的宽度大约是第二个(相同高度)的三倍.我使用 GridSpeccolspan 参数完成了此操作,但我想使用 figure 完成此操作,以便我可以保存为 PDF.我可以使用构造函数中的 figsize 参数调整第一个图形,但是如何更改第二个图形的大小?

解决方案

  • 另一种方法是使用 subplots 函数并通过 gridspec_kw 传递宽度比
      • 因为问题是规范的,所以这里有一个带有垂直子图的示例.

      # 绘制它f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})a0.plot(x, y)a1.plot(x, y)a2.plot(x, y)f.tight_layout()

      I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to do this using figure so I can save to PDF. I can adjust the first figure using the figsize argument in the constructor, but how do I change the size of the second plot?

      解决方案

      import numpy as np
      import matplotlib.pyplot as plt 
      
      # generate some data
      x = np.arange(0, 10, 0.2)
      y = np.sin(x)
      
      # plot it
      f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
      a0.plot(x, y)
      a1.plot(y, x)
      
      f.tight_layout()
      f.savefig('grid_figure.pdf')
      

      • Because the question is canonical, here is an example with vertical subplots.

      # plot it
      f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})
      
      a0.plot(x, y)
      a1.plot(x, y)
      a2.plot(x, y)
      
      f.tight_layout()
      

      这篇关于Matplotlib 不同大小的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆