regarding networkx, matplotlib and pyqt

Hello everyone.

A users on the networkx mailing list posted this example which is a modification of the matplotlib-pyqt4 implementation which plots a networkx graph.

The problem I am facing with it is that two plot windows open instead of one, one is empty and the other contains the graph.

I can’t really put my finger as to why two windows open rather than just one pyqt window with the plot. Any suggestions would be appreciated.

Screenshot of the two plot windows:

http://img259.imageshack.us/img259/8722/picture1ahr.jpg

Code:

#!/usr/bin/env python

embedding_in_qt4.py — Simple Qt4 application embedding matplotlib canvases

···

Copyright © 2005 Florent Rougon

2006 Darren Dale

This file is an example program for matplotlib. It may be used and

modified with no restriction; raw copies as well as modified versions

may be distributed without limitation.

import sys, os, random

from PyQt4 import QtGui, QtCore

from numpy import arange, sin, pi

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.figure import Figure

import networkx as nx

progname = os.path.basename(sys.argv[0])

progversion = “0.1”

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.compute_initial_figure()

FigureCanvas.init(self, fig)

self.setParent(parent)

FigureCanvas.setSizePolicy(self,

QtGui.QSizePolicy.Expanding,

QtGui.QSizePolicy.Expanding)

FigureCanvas.updateGeometry(self)

def compute_initial_figure(self):

pass

class MyStaticMplCanvas(MyMplCanvas):

"""Simple canvas with a sine plot."""

def compute_initial_figure(self):

G=nx.path_graph(10)

pos=nx.spring_layout(G)

nx.draw(G,pos,ax=self.axes)

class ApplicationWindow(QtGui.QMainWindow):

def init(self):

QtGui.QMainWindow.init(self)

self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

self.setWindowTitle(“application main window”)

self.file_menu = QtGui.QMenu(’&File’, self)

self.file_menu.addAction(’&Quit’, self.fileQuit,

QtCore.Qt.CTRL + QtCore.Qt.Key_Q)

self.menuBar().addMenu(self.file_menu)

self.help_menu = QtGui.QMenu(’&Help’, self)

self.menuBar().addSeparator()

self.help_menu.addAction(’&About’, self.about)

self.menuBar().addMenu(self.help_menu)

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.main_widget.setFocus()

self.setCentralWidget(self.main_widget)

self.statusBar().showMessage(“All hail matplotlib!”, 2000)

def fileQuit(self):

self.close()

def closeEvent(self, ce):

self.fileQuit()

def about(self):

QtGui.QMessageBox.about(self, “About %s” % progname,

u"""%(prog)s version %(version)s

Copyright \N{COPYRIGHT SIGN} 2005 Florent Rougon, 2006 Darren Dale

This program is a simple example of a Qt4 application embedding matplotlib

canvases.

It may be used and modified with no restriction; raw copies as well as

modified versions may be distributed without limitation."""

% {“prog”: progname, “version”: progversion})

qApp = QtGui.QApplication(sys.argv)

aw = ApplicationWindow()

aw.setWindowTitle("%s" % progname)

aw.show()

sys.exit(qApp.exec_())

#qApp.exec_()

Hello all,

I’ve been trying to use the volume_overlay functions from
matplotlib.finance without luck.

Is anyone using it?

Below is a simple test program. When I run it, all the volume
bars have the same height.

Thanks,

Christophe

#!/usr/bin/env python

from pylab import *

from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator,
\

DayLocator, MONDAY, timezone

from matplotlib.finance import volume_overlay3

mondays = WeekdayLocator(MONDAY) # major ticks on the
mondays

alldays = DayLocator() # minor ticks on the days

weekFormatter = DateFormatter(’%b %d’) # Eg, Jan 12

dayFormatter = DateFormatter(’%d’) # Eg, 12

quotes = [(733456.33402777778, 129.5994, 130.0,
130.55000000000001, 129.5, 6178), (733456.33472222218, 129.75,
129.30000000000001, 129.75, 129.30000000000001, 9109), (733456.3354166667,
129.5, 130.0, 130.0, 129.5, 7548), (733456.3361111111, 130.0, 130.0, 130.0,
130.0, 8039), (733456.3368055555, 130.05000000000001, 130.15000000000001,
130.30000000000001, 130.05000000000001, 9633), (733456.33750000002,
130.40000000000001, 130.15000000000001, 130.59999999999999, 130.15000000000001,
2103), (733456.33819444443, 130.15000000000001, 130.44999999999999,
130.44999999999999, 130.15000000000001, 5428), (733456.33888888895,
130.44999999999999, 130.30000000000001, 130.44999999999999, 130.30000000000001,
2025), (733456.33958333335, 130.30000000000001, 130.15000000000001, 130.55000000000001,
130.15000000000001, 3429), (733456.34027777775, 128.90000000000001,
130.05000000000001, 130.09999999999999, 128.90000000000001, 1268),
(733456.34097222227, 130.09999999999999, 130.19800000000001,
130.19800000000001, 130.05000000000001, 2891), (733456.34166666667,
130.19999999999999, 130.34999999999999, 130.34999999999999, 130.19999999999999,
1093), (733456.34236111108, 130.40000000000001, 130.44999999999999, 130.5,
130.40000000000001, 1102)]

fig = figure()

fig.subplots_adjust(bottom=0.2)

ax = fig.add_subplot(111)

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

ax.xaxis.set_major_formatter(weekFormatter)

ax.xaxis.set_minor_formatter(dayFormatter)

volume_overlay3(ax, quotes)

ax.xaxis_date()

ax.autoscale_view()

setp( gca().get_xticklabels(), rotation=45,
horizontalalignment=‘right’)

show()

Hi Ala,

Hello everyone.
A users on the networkx mailing list posted this example which is a
modification of the matplotlib-pyqt4 implementation which plots a networkx
graph.
The problem I am facing with it is that two plot windows open instead of
one, one is empty and the other contains the graph.
I can't really put my finger as to why two windows open rather than just one
pyqt window with the plot. Any suggestions would be appreciated.
Screenshot of the two plot windows:
http://img259.imageshack.us/img259/8722/picture1ahr.jpg
Code:

#!/usr/bin/env python

# embedding_in_qt4.py --- Simple Qt4 application embedding matplotlib
canvases

#

# Copyright (C) 2005 Florent Rougon

# 2006 Darren Dale

#

# This file is an example program for matplotlib. It may be used and

# modified with no restriction; raw copies as well as modified versions

# may be distributed without limitation.

import sys, os, random

from PyQt4 import QtGui, QtCore

from numpy import arange, sin, pi

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
FigureCanvas

from matplotlib.figure import Figure

import networkx as nx

progname = os.path.basename(sys.argv[0])

progversion = "0.1"

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\.compute\_initial\_figure\(\)

    \#

    FigureCanvas\.\_\_init\_\_\(self, fig\)

    self\.setParent\(parent\)

    FigureCanvas\.setSizePolicy\(self,

                              QtGui\.QSizePolicy\.Expanding,

                              QtGui\.QSizePolicy\.Expanding\)

    FigureCanvas\.updateGeometry\(self\)

def compute\_initial\_figure\(self\):

    pass

class MyStaticMplCanvas(MyMplCanvas):

"""Simple canvas with a sine plot\."""

def compute\_initial\_figure\(self\):

    G=nx\.path\_graph\(10\)

    pos=nx\.spring\_layout\(G\)

    nx\.draw\(G,pos,ax=self\.axes\)

I think this must be the problem. It looks like networkx is providing
or building upon the pylab interface, which takes care of creating
windows for you. So you are mixing with the object oriented interface
from MyMplCanvas, which should not be done. If you want to embed
matplotlib in a GUI application, you need to stick with the object
oriented interface. Something like:

    self.axes.some_plot_command(...)

Darren

···

On Mon, Jul 20, 2009 at 11:44 AM, Ala Al-Shaibani<shaibani@...2693...> wrote: