首页 > 解决方案 > Matplotlib 使用子图后创建多个图形

问题描述

问题:首先
,我用. 绘制数据后,图形将被保存。 接下来,我在创建第二个图形B之前调用(也带有)。但是,这次我用来显示 fig B。不幸的是,matplotlib 创建了两个窗口,一个用于A,一个用于B,而只有一个用于B(请参阅屏幕截图)。matplotlib.pyplot.subplots
matplotlib.pyplot.clfmatplotlib.pyplot.subplotsmatplotlib.pyplot.show


有没有办法把图A的窗口挡住?

系统信息
OS:Windows 10(64bit)
Python 版本:3.7.3
Matplotlib 版本:3.1.0

示例代码

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 100)

# Creation of figure A
fig, (ax0, ax1) = plt.subplots(2, 1)
ax0.plot(x, np.sin(x))
ax1.plot(x, np.cos(x))
fig.savefig('test.pdf')
# Removing current figure (should be figure A)
plt.clf()

# Creating second figure B
fig2, (ax_0, ax_1) = plt.subplots(2, 1)
ax_0.plot(x, np.sin(x))
ax_1.plot(x, np.cos(x))

plt.show() # Creates two windows, although I am only interested in fig2

标签: pythonmatplotlibsubplot

解决方案


推荐阅读