Matplotlib不同大小的子图 [英] Matplotlib different size subplots
本文介绍了Matplotlib不同大小的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在一个图形上添加两个子图.一个子图需要大约是第二个子图的三倍(相同的高度).我使用GridSpec
和colspan
参数完成了此操作,但我想使用figure
进行此操作,因此可以保存为PDF.我可以使用构造函数中的figsize
参数调整第一个图形,但是如何更改第二个图形的大小?
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?
推荐答案
另一种方法是使用subplots
函数并通过gridspec_kw
传递宽度比:
Another way is to use the subplots
function and pass the width ratio with gridspec_kw
:
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')
这篇关于Matplotlib不同大小的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文