-
Notifications
You must be signed in to change notification settings - Fork 0
/
mplwidget.py
60 lines (37 loc) · 1.78 KB
/
mplwidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Modified version of the file 'mplwidget.py' on http://wiki.scipy.org/Cookbook/Matplotlib/Qt_with_IPython_and_Designer
# Ajayrama Kumaraswamy, 25. 09. 2015
from PyQt4 import QtGui, QtCore
import matplotlib
matplotlib.use('Agg')
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MatplotlibWidget(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, name=None, width=5, height=4, dpi=100, bgcolor=None, mplPars=None):
self.parent = parent
if self.parent:
bgc = parent.palette().color(QtGui.QPalette.Background)
bgcolor = float(bgc.red())/255.0, float(bgc.green())/255.0, float(bgc.blue())/255.0
#bgcolor = "#%02X%02X%02X" % (bgc.red(), bgc.green(), bgc.blue())
if mplPars:
matplotlib.rcParams.update(mplPars)
self.fig = Figure(figsize=(width, height), dpi=dpi, facecolor=bgcolor, edgecolor=bgcolor)
self.axes = self.fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def sizeHint(self):
w = self.fig.get_figwidth()
h = self.fig.get_figheight()
return QtCore.QSize(w, h)
def minimumSizeHint(self):
return QtCore.QSize(10, 10)
def compute_initial_figure(self):
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
self.axes.plot(t, s)