[PyQt] PyQt with Matplotlib: stacked bar problem

Roger Oberholtzer roger at opq.se
Thu Feb 10 07:15:46 GMT 2011


On Wed, 2011-02-09 at 16:48 +0100, Massimiliano Costacurta wrote:
> Silly me, it was all due to this line of code:
>  
> self.axes.hold(False)
>  
> By removing it of course the problem disappears.
> Excuse me....I’m pretty new to matplotlib Imbarazzato
>  

I'm new to all this as well. So I am currently lurking trying to see
what folk are doing. I run various bits of code to see that I can learn.
So of course I tried to run yours (in a file called matplotlib.py). I
get:

Traceback (most recent call last):
  File "matplotlib.py", line 6, in <module>
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  File "/home/roger/PyQt/matplotlib.py", line 6, in <module>
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
ImportError: No module named backends.backend_qt4agg


And yes, the error is printed twice as shown.




> From: massi_srb at msn.com 
> Sent: Wednesday, February 09, 2011 4:31 PM
> To: pyqt at riverbankcomputing.com 
> Subject: PyQt with Matplotlib: stacked bar problem
>  
> Hi everyone,
>  
> in my script I’m trying to display a stacked bar graph using
> matplotlib (0.99.1). Here is a piece of runnable code showing what I’m
> aiming to do:
>  
> import sys, os, random
> from PyQt4 import QtGui, QtCore
>  
> import numpy
> from numpy import arange, sin, pi
> from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
> FigureCanvas
> from matplotlib.figure import Figure
>  
> progname = os.path.basename(sys.argv[0])
>  
> class MyMplCanvas(FigureCanvas):
>     """Ultimately, this is a QWidget (as well as a FigureCanvasAgg,
> etc.)."""
>     def __init__(self, parent=None, width=5, height=4, dpi=100):
>         fig = Figure(figsize=(width, height), dpi=dpi)
>         self.axes = fig.add_subplot(111)
>         # We want the axes cleared every time plot() is called
>         self.axes.hold(False)
>  
>         self.figure = fig
>         FigureCanvas.__init__(self, fig)
>         self.setParent(parent)
>  
>         FigureCanvas.setSizePolicy(self,
>                                    QtGui.QSizePolicy.Expanding,
>                                    QtGui.QSizePolicy.Expanding)
>         FigureCanvas.updateGeometry(self)
>         self.compute_initial_figure()
>  
>  
> class MyStaticMplCanvas(MyMplCanvas):
>     """Simple canvas with a sine plot."""
>     def compute_initial_figure(self):
>         N = 10
>         A = numpy.array([70, 88, 78, 93, 99, 58, 89, 66, 77, 78])
>         B = numpy.array([73, 65, 78, 87, 97, 57, 77, 88, 69, 78])
>         C = numpy.array([66, 98, 88, 67, 99, 88, 62, 70, 90, 73])
>  
>         ind = numpy.arange(N)
>         width = 0.35
>  
>         p1 = self.axes.bar(ind, A,width, color='r')
>         p2 = self.axes.bar(ind, B, width, color='y', bottom=A)
>         p3 = self.axes.bar(ind, C, width, color='b', bottom=A+B)
>  
> class ApplicationWindow(QtGui.QMainWindow):
>     def __init__(self):
>         QtGui.QMainWindow.__init__(self)
>         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
>         self.setWindowTitle("application main window")
>  
>         self.main_widget = QtGui.QWidget(self)
>  
>         l = QtGui.QVBoxLayout(self.main_widget)
>         sc = MyStaticMplCanvas(self.main_widget, width=5, height=4,
> dpi=100)
>         l.addWidget(sc)
>  
>         self.setCentralWidget(self.main_widget)
>  
> qApp = QtGui.QApplication(sys.argv)
>  
> aw = ApplicationWindow()
> aw.show()
> sys.exit(qApp.exec_())
>  
> if you run the code you will see that the bars are not stacked, since
> only the last graph is shown. Is it a bug or am I missing something?
> if so can anyone point me out what I am doing wrong? Thanks in
> advance.
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Yours sincerely,

Roger Oberholtzer

OPQ Systems / Ramböll RST

Office: Int +46 10-615 60 20
Mobile: Int +46 70-815 1696
roger.oberholtzer at ramboll.se
________________________________________

Ramböll Sverige AB
Krukmakargatan 21
P.O. Box 17009
SE-104 62 Stockholm, Sweden
www.rambollrst.se



More information about the PyQt mailing list