Matplotlib 将子图绘制到现有图形
Matplotlib plotting subplots to existing figure
我想知道是否有与
等效的功能
fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')
其中仅为现有图形生成轴数组,而不是创建新的图形对象。我基本上需要创建一个 matplotlib window,填充它,并在按下按钮时更新它。我想使用 subplots
方法,因为它允许共享轴,但它会强制创建一个新的图形对象,这将打开一个新的 window.
我当前的代码如下所示:
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 windows?如果这也是一个选项,我可以将新图形对象绘制到现有 window 中。
如果有任何不同,我正在使用 PySide 后端。谢谢
此功能在主分支 via PR#5146 上实现,该分支针对 应该 秋季发布的 mpl 2.1。
如果你需要它现在 运行 主分支或供应商那个方法作为一个函数
axarr = vendored_subplots(fig, ...)
肮脏的解决方法:
您可以使用关键字 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_kw
和 subplot_kw
参数。之后就不能轻易更改了。
我想知道是否有与
等效的功能fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')
其中仅为现有图形生成轴数组,而不是创建新的图形对象。我基本上需要创建一个 matplotlib window,填充它,并在按下按钮时更新它。我想使用 subplots
方法,因为它允许共享轴,但它会强制创建一个新的图形对象,这将打开一个新的 window.
我当前的代码如下所示:
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 windows?如果这也是一个选项,我可以将新图形对象绘制到现有 window 中。
如果有任何不同,我正在使用 PySide 后端。谢谢
此功能在主分支 via PR#5146 上实现,该分支针对 应该 秋季发布的 mpl 2.1。
如果你需要它现在 运行 主分支或供应商那个方法作为一个函数
axarr = vendored_subplots(fig, ...)
肮脏的解决方法:
您可以使用关键字 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_kw
和 subplot_kw
参数。之后就不能轻易更改了。