Embedding a networkx graph into wxPython with matp

2019-04-12 11:09发布

问题:

I've created a graph with networkx.

G=nx.DiGraph()

# ... building the graph ...
# and I can display it with matplotlib like this:

nx.draw(G)
matplotlib.pyplot.show()

But what I would like to do is, starting from the following example, to embed the created graph into wxPython. For the beginning I would like just to print it; no user interaction at all.

from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

import wx

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    def draw(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    fr = wx.Frame(None, title='test')
    panel = CanvasPanel(fr)
    panel.draw()
    fr.Show()
    app.MainLoop()

Can someone give me a tip ?

回答1:

Workaround: export the networkx graph as a png and plot it using imread.

plt.axis("off") # turn off axis
# output to a temporary file name and display it in matplotlib     
filename = "/tmp/image.png"
plt.savefig(filename, dpi=400, bbox_inches='tight') 
img = imread(filename)
self.axes.imshow(img)


回答2:

Here what I did, you can use plt.figure() to get the instance of the figure and glue that into the canvas, then add the canvas to the panel using e.g., vbox:

import matplotlib.pyplot as plt
import networkx as nx
import wx
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar

class NetworkFrame(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, -1)
    self.panel = wx.Panel(self)
    self.fig = plt.figure()
    self.canvas = FigCanvas(self.panel, -1, self.fig)
    G=nx.house_graph()
pos={0:(0,0),
    1:(1,0),
    2:(0,1),
    3:(1,1),
    4:(0.5,2.0)}

nx.draw_networkx_nodes(G,pos,node_size=2000,nodelist=[4])
nx.draw_networkx_nodes(G,pos,node_size=3000,nodelist=[0,1,2,3],node_color='b')
nx.draw_networkx_edges(G,pos,alpha=0.5,width=6)
plt.axis('off')
self.vbox = wx.BoxSizer(wx.VERTICAL)
    self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
    self.toolbar = NavigationToolbar(self.canvas)
    self.vbox.Add(self.toolbar, 0, wx.EXPAND)
    self.panel.SetSizer(self.vbox)
    self.vbox.Fit(self)


#plt.savefig("house_with_colors.png") # save as png
#plt.show() # display

if __name__ == '__main__':
  app = wx.PySimpleApp()
  app.frame = NetworkFrame()
  app.frame.Show()
  app.MainLoop()