Matplotlib绘制子图到现有图

时间:2016-07-06 18:56:01

标签: matplotlib subplot

我想知道是否有与

相同的功能
fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')

其中仅为现有图形生成轴数组,而不是创建新的图形对象。我基本上需要创建一个matplotlib窗口,填充它,并在按下按钮时更新它。我想使用subplots方法,因为它允许共享轴,但它强制创建一个新的图形对象,这将打开一个新窗口。

我目前的代码如下:

fig = plt.figure()

# When button is pressed
fig.clear()
for i in range(6):
    ax = fig.add_subplot(3, 2, i+1)
    ax.plot(x, y)
plt.draw()

我想做点什么

fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')

# When button is pressed
fig.clear()
axarr = fig.subplots(3, 2, sharex='col', sharey='row')
for i in range(3):
    for j in range(2):
        ax = axarr[i][j]
        ax.plot(x, y)
plt.draw()

或者,有没有办法直接使用matplotlib窗口?如果这也是一个选项,我可以将新的图形对象绘制到现有窗口中。

如果这有任何区别,我正在使用PySide后端。感谢。

2 个答案:

答案 0 :(得分:1)

此功能在主分支via PR#5146上实现,主分支针对mpl 2.1,应该在秋季出局。

如果您需要它现在运行主分支或供应商该方法作为函数

axarr = vendored_subplots(fig, ...)

答案 1 :(得分:0)

肮脏的解决方法:

您可以使用关键字num明确指定数字句柄:

import matplotlib.pyplot as plt
fig1, axes1 = plt.subplots(5, 4, gridspec_kw={'top': 0.5}, num=1)
fig2, axes2 = plt.subplots(5, 4, gridspec_kw={'bottom': 0.5}, num=1)
plt.show()

这给出了:

In [2]: fig1 is fig2
Out[2]: True

In [3]: axes1 is axes2
Out[3]: False

所有子图参数必须在开头提供给grispec_kwsubplot_kw参数。之后他们就不能轻易改变了。

相关问题