【发布时间】:2016-11-08 23:04:32
【问题描述】:
我想知道是否有等效的功能
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 后端。谢谢。
【问题讨论】:
-
据我所知,您要么使用
fig, axarr = plt.subplots(...),要么自己创建图形并手动添加子图。如果我没记错的话,plt.subplots实际上是在 pyplot 源代码的引擎盖下为您添加每个新轴,即根据您的第一个示例。 -
是的,但是
subplots方法能够隐藏某些轴标签,并且在使用缩放等交互式工具时还可以将某些子图链接在一起。我不确定如何复制这些行为。 -
如果您想将两个子图链接在一起,您可以在调用
add_subplot时使用关键字参数sharex = ax_???或sharey来实现。 IE。ax2 = fig.add_subplots(2, 1, 2, sharex=ax1)如果 ax1 已经创建。
标签: matplotlib subplot