Last active
August 29, 2015 14:02
-
-
Save jbmohler/7c5c8cca39826ea8ede7 to your computer and use it in GitHub Desktop.
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
import time | |
import numpy | |
from PySide import QtCore, QtGui | |
import matplotlib | |
import matplotlib.ticker | |
matplotlib.use('Qt4Agg') | |
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas | |
from matplotlib.figure import Figure | |
class Graph(QtGui.QWidget): | |
def __init__(self, parent=None): | |
super(Graph, self).__init__(parent) | |
self.figure = Figure(figsize=(600, 600), dpi=72, facecolor=(1, 1, 1), edgecolor=(0, 0, 0), tight_layout=True) | |
self.canvas = FigureCanvas(self.figure) | |
self.ax = self.figure.add_subplot(1, 1, 1) | |
self.layout = QtGui.QVBoxLayout(self) | |
self.header = QtGui.QHBoxLayout() | |
self.form = QtGui.QFormLayout() | |
self.num_points_edit = QtGui.QLineEdit() | |
self.num_points_edit.setText('100') | |
self.num_points_edit.editingFinished.connect(self.present_axis) | |
self.num_ticks_edit = QtGui.QLineEdit() | |
self.num_ticks_edit.setText('10') | |
self.num_ticks_edit.editingFinished.connect(self.present_axis) | |
self.form.addRow('Points', self.num_points_edit) | |
self.form.addRow('Ticks', self.num_ticks_edit) | |
self.header.addLayout(self.form) | |
self.commands = QtGui.QDialogButtonBox(QtCore.Qt.Vertical) | |
self.commands.addButton('Test FPS', QtGui.QDialogButtonBox.ActionRole).clicked.connect(self.test_fps) | |
self.header.addWidget(self.commands) | |
self.layout.addLayout(self.header) | |
self.layout.addWidget(self.canvas) | |
def present_axis(self): | |
self.points = int(self.num_points_edit.text()) | |
self.ticks = int(self.num_ticks_edit.text()) | |
if hasattr(self, 'scat'): | |
self.scat.remove() | |
self.scat = self.ax.scatter(numpy.arange(self.points), numpy.sin(numpy.arange(self.points))) | |
self.ax.xaxis.set_minor_locator(matplotlib.ticker.LinearLocator(self.ticks)) | |
self.ax.yaxis.set_minor_locator(matplotlib.ticker.LinearLocator(self.ticks)) | |
self.canvas.draw() | |
def test_fps(self): | |
self.start_test = time.time() | |
self.test_count = 0 | |
QtCore.QTimer.singleShot(0, self._internal_test) | |
def _internal_test(self): | |
now = time.time() | |
if now - self.start_test > 1.: | |
print 'Points: {}; Ticks: {}; FPS: {}'.format(self.points, self.ticks, self.test_count) | |
return | |
self.canvas.draw() | |
self.test_count += 1 | |
QtCore.QTimer.singleShot(0, self._internal_test) | |
app = QtGui.QApplication([]) | |
w = QtGui.QMainWindow() | |
w.setCentralWidget(Graph()) | |
w.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script illustrates performance problems related to the quantity of tick-marks on a matplotlib axis. Redraw time is roughly doubled for every 10-fold increase in tick-marks. By contrast, number of points in a scatter plot has very little impact on redraw time.