source: bcdaqwidgets/trunk/src/_testing/MatplotlibWidget.py @ 1442

Last change on this file since 1442 was 1442, checked in by jemian, 9 years ago

re-arrangement

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision Author HeadURL Id
File size: 4.3 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright © 2009 Pierre Raybaut
4# Licensed under the terms of the MIT License
5
6# source:
7#   http://code.google.com/p/pythonxy/source/browse/src/python/matplotlib/QtDesigner_Plugins/matplotlibwidget.py?r=640d80c9867ba5f00cd0734c87da0ebd938b5d76
8
9"""
10MatplotlibWidget
11================
12
13Modified example of matplotlib widget for PySide
14
15Copyright © 2009 Pierre Raybaut
16This software is licensed under the terms of the MIT License
17
18Derived from 'embedding_in_pyqt4.py':
19Copyright © 2005 Florent Rougon, 2006 Darren Dale
20"""
21
22__version__ = "1.0.0"
23
24from PySide.QtGui import QSizePolicy
25from PySide.QtCore import QSize
26
27import matplotlib
28matplotlib.rcParams['backend.qt4']='PySide'
29
30from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as Canvas
31from matplotlib.figure import Figure
32
33from matplotlib import rcParams
34rcParams['font.size'] = 9
35
36
37class MatplotlibWidget(Canvas):
38    """
39    MatplotlibWidget inherits PySide.QtGui.QWidget
40    and matplotlib.backend_bases.FigureCanvasBase
41   
42    Options: option_name (default_value)
43    -------   
44    parent (None): parent widget
45    title (''): figure title
46    xlabel (''): X-axis label
47    ylabel (''): Y-axis label
48    xlim (None): X-axis limits ([min, max])
49    ylim (None): Y-axis limits ([min, max])
50    xscale ('linear'): X-axis scale
51    yscale ('linear'): Y-axis scale
52    width (4): width in inches
53    height (3): height in inches
54    dpi (100): resolution in dpi
55    hold (False): if False, figure will be cleared each time plot is called
56   
57    Widget attributes:
58    -----------------
59    figure: instance of matplotlib.figure.Figure
60    axes: figure axes
61   
62    Example:
63    -------
64    self.widget = MatplotlibWidget(self, yscale='log', hold=True)
65    from numpy import linspace
66    x = linspace(-10, 10)
67    self.widget.axes.plot(x, x**2)
68    self.widget.axes.plot(x, x**3)
69    """
70    def __init__(self, parent=None, title='', xlabel='', ylabel='',
71                 xlim=None, ylim=None, xscale='linear', yscale='linear',
72                 width=4, height=3, dpi=100, hold=False,
73                 **kws):
74        self.figure = Figure(figsize=(width, height), dpi=dpi)
75        self.axes = self.figure.add_subplot(111)
76        self.axes.set_title(title)
77        self.axes.set_xlabel(xlabel)
78        self.axes.set_ylabel(ylabel)
79        if xscale is not None:
80            self.axes.set_xscale(xscale)
81        if yscale is not None:
82            self.axes.set_yscale(yscale)
83        if xlim is not None:
84            self.axes.set_xlim(*xlim)
85        if ylim is not None:
86            self.axes.set_ylim(*ylim)
87        self.axes.hold(hold)
88        # with kws, can pass in other items
89        self.axes.grid(kws.pop('showgrid', False))
90        for key, value in kws.items():
91            func = self.axes.__getattribute__('set_'+key)
92            if func is not None:
93                func(value)
94
95        Canvas.__init__(self, self.figure)
96        self.setParent(parent)
97
98        Canvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
99        Canvas.updateGeometry(self)
100
101    def addPlot(self, *args, **kws):
102        p1, = self.axes.plot(*args, **kws)
103        return p1
104
105    def sizeHint(self):
106        w, h = self.get_width_height()
107        return QSize(w, h)
108
109    def minimumSizeHint(self):
110        return QSize(10, 10)
111
112
113
114#===============================================================================
115#   Example
116#===============================================================================
117if __name__ == '__main__':
118    import sys
119    from PySide.QtGui import QMainWindow, QApplication
120    from numpy import linspace
121   
122    class ApplicationWindow(QMainWindow):
123        def __init__(self):
124            QMainWindow.__init__(self)
125            self.mplwidget = MatplotlibWidget(self, title='Example',
126                                              xlabel='Linear scale',
127                                              ylabel='Log scale',
128                                              hold=True, yscale='log')
129            self.mplwidget.setFocus()
130            self.setCentralWidget(self.mplwidget)
131            self.plot(self.mplwidget.axes)
132           
133        def plot(self, axes):
134            x = linspace(-10, 10)
135            axes.plot(x, x**2)
136            axes.plot(x, x**3)
137       
138    app = QApplication(sys.argv)
139    win = ApplicationWindow()
140    win.show()
141    sys.exit(app.exec_())
142
Note: See TracBrowser for help on using the repository browser.