我在尝试从画布中删除几个子轴时遇到了麻烦。下面的示例脚本显示了该问题:
from matplotlib.figure import Figure
from mpl_toolkits.axes_grid.axes_divider import make_axes_locatable
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Test(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(1000, 600)
self.fig = Figure(figsize=(100,100), dpi=75)
axes1 = self.fig.add_subplot(121)
axes2 = self.fig.add_subplot(122)
self.subaxes = []
for ax in [axes1, axes2]:
divider = make_axes_locatable(ax)
self.subaxes.append(divider.append_axes("right", "40%", "20%"))
b = QPushButton("Remove subaxes")
b.clicked.connect(self.OnBtnClicked)
l = QHBoxLayout()
l.addWidget(FigureCanvas(self.fig))
l.addWidget(b)
self.setLayout(l)
def OnBtnClicked(self):
for sa in self.subaxes:
self.fig.delaxes(sa)
del sa
self.fig.canvas.draw()
app = QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())
主窗口由两个主轴和位于其右侧的两个子轴组成。如果单击窗口中的按钮,两个子轴会按我的预期消失,但原始轴不会调整大小。有什么我想念的吗?或者任何人都可以指出我是否有更好的方法来实现这一目标?
提前致谢!