我目前正在尝试改进绘制软件的一部分。
我们正在使用WXPython和Matplotlib来向用户显示许多包含各种控件的图。
总结一下,这是上下文:
Matplotlib性能适用于我们的绘图需求,但是,调整我们的wxpython主框架的大小是调整包含matplotlib画布的wx面板。
(在每一步或调整大小,而不仅仅是在结束时)
如果速度很快调整面板大小,但matplotlib也会在调整大小的每个步骤重绘画布和数字,从而产生视觉冻结和一些延迟。
总结一下,问题是:
我想知道是否有办法在我们的WX Frame resize事件期间禁用(临时)matplotlib事件/自动重绘事件,然后在其末尾应用重绘。
有什么想法吗?
答案 0 :(得分:0)
这只是答案的一半,因为(1)我在这里使用PyQt而不是WX,(2)它只显示如何防止调整大小的发生,以便以后可以手动完成。
我们的想法是继承FigureCanvas
并接管resizeEvent
方法。在这种方法中只存储resize事件,但不要让它发生
稍后手动触发事件。
import sys
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
class MyFigureCanvas(FigureCanvas):
""" Subclass canvas to catch the resize event """
def __init__(self, figure):
self.lastEvent = False # store the last resize event's size here
FigureCanvas.__init__(self, figure)
def resizeEvent(self, event):
if not self.lastEvent:
# at the start of the app, allow resizing to happen.
super(MyFigureCanvas, self).resizeEvent(event)
# store the event size for later use
self.lastEvent = (event.size().width(),event.size().height())
print "try to resize, I don't let you."
def do_resize_now(self):
# recall last resize event's size
newsize = QtCore.QSize(self.lastEvent[0],self.lastEvent[1] )
# create new event from the stored size
event = QtGui.QResizeEvent(newsize, QtCore.QSize(1, 1))
print "Now I let you resize."
# and propagate the event to let the canvas resize.
super(MyFigureCanvas, self).resizeEvent(event)
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.main_widget = QtGui.QWidget(self)
l = QtGui.QVBoxLayout(self.main_widget)
self.fig = Figure()
# use subclassed FigureCanvas
self.canvas = MyFigureCanvas(self.fig)
self.button = QtGui.QPushButton("Manually Resize")
l.addWidget(self.button)
l.addWidget(self.canvas)
self.setCentralWidget(self.main_widget)
self.button.clicked.connect(self.action)
self.plot()
def action(self):
# when button is clicked, resize the canvas.
self.canvas.do_resize_now()
def plot(self):
# just some random plots
self.axes = []
for i in range(4):
ax = self.fig.add_subplot(2,2,i+1)
a = np.random.rand(100,100)
ax.imshow(a)
self.axes.append(ax)
self.fig.tight_layout()
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())
可能在WX中这样做并没有太大的不同。