[PyQt] PyQt with Matplotlib: stacked bar problem

Massimiliano Costacurta massi_srb at msn.com
Wed Feb 9 15:48:27 GMT 2011


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 

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110209/3de07763/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wlEmoticon-embarrassedsmile[1].png
Type: image/png
Size: 1151 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110209/3de07763/attachment-0001.png>


More information about the PyQt mailing list