如何在连续的 matplotlib 图形之间添加 space?

How to add space between sequential matplotlib figures?

如何在 JupyterLab 中的同一个 Jupyter notebook 单元格中的多个图形之间添加 space? 为清楚起见,我不是要在子图之间添加 space,而是要在数字之间添加。

例如,

import matplotlib.pyplot as plt

fig1 = plt.figure()
_ = plt.plot(x,y)

fig2 = plt.figure()
_ = plt.hist(z)

将有两个数字 'attached' 我想在它们之间添加 space 类似于 print('\n') 在打印输出之间添加 space 的方式。

为了进一步说明,使用:

import matplotlib.pyplot as plt

fig1 = plt.figure()
_ = plt.plot([1, 2], [2, 3])

print('\n' * 4)

fig2 = plt.figure()
_ = plt.hist([1, 2, 3])

在 JupyterLab 中导致新行被放置在图的前面,而不是介于:

一种方法是捕获 ipywidgets 输出中的图:

import matplotlib.pyplot as plt
import ipywidgets as widgets

out1 = widgets.Output()
out2 = widgets.Output()
spacer = widgets.Output()

with out1:
    fig1 = plt.figure()
    _ = plt.plot([1, 2], [2, 3])
    plt.show()

with out2:
    fig2 = plt.figure()
    _ = plt.hist([1, 2, 3])
    plt.show()

with spacer:
    print('\n' * 4)

widgets.VBox([out1, spacer, out2])

这导致:

只需添加一个空白图形即可在图形之间创建space:

import matplotlib.pyplot as plt

fig1 = plt.figure()
_ = plt.plot([1, 2], [2, 3])

f,ax = plt.subplots()
f.set_visible(False)
f.set_figheight(2) # figure height in inches

fig2 = plt.figure()
_ = plt.hist([1, 2, 3])

并用f.set_figheight()

控制间距

To produce this figure