0

I have to plot a grid of subplots (16x16 ones, for example). I use matplotlib but It is a bit too slow, especially when I have to redraw the plots (for example, when their size is changed). How can I make it faster?

P.S. Here is an example of code:

import sys
import matplotlib
matplotlib.use('Qt4Agg')
import pylab

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import PyQt4
from PyQt4 import QtCore, QtGui, Qt

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    # generate the plot
    fig = Figure(figsize=(800, 800), facecolor=(1, 1, 1), edgecolor=(0, 0, 0))

    for i in range(256):
        ax = fig.add_subplot(16, 16, i)
        ax.plot([0, 1])
        ax.set_xticks([])
        ax.set_yticks([])
#        ax.set_title("Mega %i" % (i,))

    # generate the canvas to display the plot
    canvas = FigureCanvas(fig)
    canvas.setMinimumWidth(640)
    canvas.setMinimumHeight(640)

    # generate layout
    layout = QtGui.QVBoxLayout();
    layout.addWidget(canvas)
    layout.setGeometry(QtCore.QRect(0, 0, 1000, 1000))

    # generate widget
    widget = QtGui.QWidget()
    widget.setLayout(layout)

    # generate scroll area
    win = QtGui.QScrollArea()
    win.setWidget(widget)
    win.setMinimumWidth(100)
    win.setWidgetResizable(True)
    win.show()

    canvas.draw()

    sys.exit(app.exec_())
Felix
  • 3,351
  • 6
  • 40
  • 68

1 Answers1

2

I don't have an environment to test your code but I these steps work for me:

  1. Using cProfile to profile the whole thing (f.e. How can you profile a python script?).

  2. Usually its one or two functions which slow down all.

  3. Search stackoverflow/the internet for these function names. Usually 1-2 people solved the performance issue already.

Greetings Kuishi

Community
  • 1
  • 1
Kuishi
  • 157
  • 4