Here is a slightly modified version that draws a graph with NetworkX.
Aric
#!/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)
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.menuBar().addMenu(self.help_menu)
self.help_menu.addAction('&About', self.about)
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_()
Not sure, I only get one plot when I run that code.
If you can't figure it out you might try
asking on the matplotlib users mailing list.
Aric
line, = ax.plot(np.random.rand(100), 'o', picker=5) # 5 points tolerance
Matplotlib provides this functionality:
>>> line, = ax.plot(np.random.rand(100), 'o')
>>> line.set_picker(5)
Chris
Apologies for the cryptic response. The following (or something
similar) is what I had in mind:
>>> import matplotlib.pyplot as plt
>>> import networkx as nx
>>> g = nx.generators.bull_graph()
>>> nx.draw(g)
>>> ax = plt.gca()
>>> ax.set_picker(5)
> Chris
I found the following page quite useful:
http://matplotlib.sourceforge.net/users/event_handling.html
To do what you seek, we need access to the matplotlib artist that is
created when networkx draws your graph. Once you have that, everything
else can be found in the matplotlib documentation.
I've attached an example,
Chris
Well "integration" is is probably not the word I would use
to descibe how networkx interacts with matplotlib :-/.
It is a hack that could be improved a lot.
Nevertheless matplotlib is great and you can do what you want like this.
You have to draw the nodes, edges, and labels separately so you
can get access to the matplotlib patch collection to set the picker.
Aric
from pylab import figure, show
import networkx as nx
fig = figure()
ax1 = fig.add_subplot(111)
g = nx.Graph()
g.add_path([0, 1, 2])
pos = nx.spring_layout(g)
nodes=nx.draw_networkx_nodes(g, pos, ax= ax1)
nodes.set_picker(True)
edges=nx.draw_networkx_edges(g, pos, ax= ax1)
locs = ['uk', 'france', 'usa']
labels=dict(zip(range(3),locs))
nx.draw_networkx_labels(g, pos, ax= ax1,labels=labels)
def onpick(event):
ind=event.ind[0]
print labels[ind]
return True