Created
March 31, 2014 20:20
-
-
Save iMichka/9901318 to your computer and use it in GitHub Desktop.
MPL blitting problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import numpy | |
from PyQt4 import QtGui | |
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg | |
from matplotlib.collections import PolyCollection | |
from matplotlib.figure import Figure | |
class MainWindow(QtGui.QMainWindow): | |
def __init__(self): | |
QtGui.QMainWindow.__init__(self) | |
self.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum) | |
subw = SubWidget() | |
tabs = QtGui.QTabWidget() | |
tabs.addTab(subw, "Data") | |
self.setCentralWidget(tabs) | |
class SubWidget(QtGui.QWidget): | |
def __init__(self): | |
super(QtGui.QWidget, self).__init__() | |
VL = QtGui.QVBoxLayout() | |
mesh_size = [300, 300] | |
canvas = QtGui.QWidget() | |
sizes = [mesh_size[0]/72.0, mesh_size[1]/72.0, 72.0] | |
self.myplot = MyPlot(self, canvas, sizes) | |
self.myplot.mpl_connect("button_press_event", self.mesh_press) | |
VL.addWidget(self.myplot) | |
self.setLayout(VL) | |
self.myplot.update_plot() | |
self.myplot.update_blit() | |
def mesh_press(self, event): | |
xpos, ypos = int(event.xdata), int(event.ydata) | |
self.myplot.xpos = xpos | |
self.myplot.ypos = ypos | |
self.myplot.update_blit() | |
class MyPlot(FigureCanvasQTAgg): | |
def __init__(self, parent, canvas, sizes): | |
self.parent = parent | |
self.canvas = canvas | |
self.xpos = 0 | |
self.ypos = 0 | |
self.fig = Figure(figsize = (sizes[0], sizes[1]), dpi = sizes[2]) | |
FigureCanvasQTAgg.__init__(self, self.fig) | |
FigureCanvasQTAgg.setSizePolicy(self, | |
QtGui.QSizePolicy.MinimumExpanding, | |
QtGui.QSizePolicy.MinimumExpanding) | |
FigureCanvasQTAgg.updateGeometry(self) | |
self.setParent(canvas) | |
def update_plot(self): | |
self.fig.clf() | |
self.axes = self.fig.add_subplot(111) | |
array = numpy.random.randint(10, size=(20, 20)) | |
self.axes.pcolormesh(array, cmap = "copper") | |
# Draw in the canvas | |
self.fig.canvas.draw() | |
self.empty_bbox = self.copy_from_bbox(self.axes.bbox) | |
def update_blit(self): | |
self.restore_region(self.empty_bbox) | |
self.draw_red_square() | |
self.blit(self.axes.bbox) | |
def draw_red_square(self): | |
x_size = 1 | |
y_size = 1 | |
pos = [self.xpos, self.ypos] | |
x1 = pos[0]*x_size + x_size | |
y1 = pos[1]*y_size + y_size | |
x2 = pos[0]*x_size + x_size | |
y2 = pos[1]*y_size | |
x3 = pos[0]*x_size | |
y3 = pos[1]*y_size | |
x4 = pos[0]*x_size | |
y4 = pos[1]*y_size + y_size | |
verts = numpy.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)]) | |
coll = PolyCollection([verts], facecolors = "none", | |
edgecolors = "r", linewidths = 2) | |
self.axes.add_collection(coll) | |
self.axes.draw_artist(coll) | |
def main(): | |
app = QtGui.QApplication(["Test"]) | |
main_window = MainWindow() | |
main_window.show() | |
if sys.platform == "darwin": | |
main_window.raise_() | |
app.exec_() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment