我有以下代码:

fig = plt.figure(figsize = (16,9))   
ax1 = plt.subplot(3, 2, 1)
ax2 = plt.subplot(3, 2, 2)
ax3 = plt.subplot(3, 2, 3)
ax4 = plt.subplot(3, 2, 4)
ax5 = plt.subplot(3, 1, 3)

enter image description here

然而,我希望第三行(跨越两列的绘图)的高度减少一半.我该怎么做?

我知道这篇帖子:Matplotlib different size subplots,但我不明白如何更改跨越两列的最低绘图的高度.

为了避免混淆:我想把下面的图一分为二(删除红色部分),从而使它变小(按高度).

enter image description here

推荐答案

您链接到的帖子的答案将对您有所帮助,但目前最简单的方法(自matplotlib-3.3.0以来)可能是使用plt.subplot_mosaic,并将高度比传递给gridspec_kw参数.

例如

fig, axes = plt.subplot_mosaic("AB;CD;EE", gridspec_kw=dict(height_ratios=[1, 1, 0.5]))

这会给你

enter image description here

This returns axes as a dictionary of Axes with the name you give it, so you can plot with 例如

axes["A"].plot([1, 2, 3], [1, 2, 3])

(您也可以像这样"双叠"马赛克,以获得相同的效果,而无需使用gridspec_kw)

fig, axes = plt.subplot_mosaic("AB;AB;CD;CD;EE")

如果出于某种原因,你不想(或不能)使用suplot_mosaic,那么你可以直接使用GridSpec(example here).

from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(nrows=3, ncols=2, figure=fig, height_ratios=[1, 1, 0.5])
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])
ax5 = fig.add_subplot(gs[2, :])

这会给你 the same thing.

Python相关问答推荐

OdooElectron 商务产品详情页面中add_qty参数动态更新

判断两极中N(N 2)列水平是否相等

每个组每第n行就有Pandas

使用Python和PRNG(不是梅森龙卷风)有效地生成伪随机浮点数在[0,1)中均匀?

如何在PIL、Python中对图像应用彩色面膜?

是pandas.DataFrame使用方法查询后仍然排序吗?

如何让我的Tkinter应用程序适合整个窗口,无论大小如何?

为什么带有dropna=False的groupby会阻止后续的MultiIndex.dropna()工作?

C#使用程序从Python中执行Exec文件

将pandas Dataframe转换为3D numpy矩阵

Python虚拟环境的轻量级使用

无法使用requests或Selenium抓取一个href链接

如何获取numpy数组的特定索引值?

关于Python异步编程的问题和使用await/await def关键字

使用Python更新字典中的值

Plotly Dash Creating Interactive Graph下拉列表

如何指定列数据类型

在matplotlib中删除子图之间的间隙_mosaic

幂集,其中每个元素可以是正或负""""

Polars将相同的自定义函数应用于组中的多个列,