Matplotlib不同大小的子图

Matplotlib不同大小的子图

我需要在图中添加两个子图。一个子图需要大约是第二个(相同高度)的三倍。我使用GridSpeccolspan论证完成了这个,但我想这样做,figure所以我可以保存为PDF。我可以使用figsize构造函数中的参数调整第一个数字,但是如何更改第二个图的大小?



aluckdog
浏览 2702回答 3
3回答

隔江千里

另一种方法是使用该subplots函数并传递宽度比gridspec_kw:import numpy as npimport matplotlib.pyplot as plt # generate some datax = np.arange(0, 10, 0.2)y = np.sin(x)# plot itf, (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')

交互式爱情

你可以使用gridspec和figure:import numpy as npimport matplotlib.pyplot as plt  from matplotlib import gridspec# generate some datax = np.arange(0, 10, 0.2)y = np.sin(x)# plot itfig = plt.figure(figsize=(8, 6)) gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) ax0 = plt.subplot(gs[0])ax0.plot(x, y)ax1 = plt.subplot(gs[1])ax1.plot(y, x)plt.tight_layout()plt.savefig('grid_figure.pdf')

慕尼黑5688855

可能最简单的方法是使用subplot2grid,在使用GridSpec定制子图的位置中描述。ax = plt.subplot2grid((2, 2), (0, 0))等于import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(2, 2)ax = plt.subplot(gs[0, 0])所以bmu的例子变成了:import numpy as npimport matplotlib.pyplot as plt# generate some datax = np.arange(0, 10, 0.2)y = np.sin(x)# plot itfig = plt.figure(figsize=(8, 6))ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2)ax0.plot(x, y)ax1 = plt.subplot2grid((1, 3), (0, 2))ax1.plot(y, x)plt.tight_layout()plt.savefig('grid_figure.pdf')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python