0

I'm trying to use NetworkX1.9.1 with wxPython2.9.2.4 to visualize networks on a 10.10.2Mac, and my Python version is Python2.7.6.

I successfully visualized a network with following code:

G = nx.random_graphs.barabasi_albert_graph(100, 1)
nx.draw(G)
plt.show()

But when I tried to use NetworkX with wxPython, I'm getting some trouble.

The code is the following:

import networkx as nx
import matplotlib.pyplot as plt
import wx


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title='Test')
        bt = wx.Button(self, label='bt')
        bt.Bind(wx.EVT_BUTTON, self.Onbt)   

def Onbt(self, e):
        G = nx.random_graphs.barabasi_albert_graph(100, 1)
        nx.draw(G)
        plt.show()  


app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()

And the errors appear when I click the button:

2015-02-17 22:39:59.173 Python[7859:492618] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10fe21c00
2015-02-17 22:39:59.180 Python[7859:492618] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10fe21c00
2015-02-17 22:39:59.231 Python[7859:492618] (
    0   CoreFoundation                      0x00007fff973f666c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff90ba576e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff973f96dd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff97340aa4 ___forwarding___ + 1028
    4   CoreFoundation                      0x00007fff97340618 _CF_forwarding_prep_0 + 120
    5   Tk                                  0x000000010b541527 TkpInit + 476
    6   Tk                                  0x000000010b4bbaca Tk_Init + 1788
    7   _tkinter.so                         0x000000010a891ceb Tcl_AppInit + 75
    8   _tkinter.so                         0x000000010a88ef14 Tkinter_Create + 916
    9   Python                              0x00000001000c6ced PyEval_EvalFrameEx + 25213
    10  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    11  Python                              0x00000001000419e0 function_call + 176
    12  Python                              0x000000010000fe02 PyObject_Call + 98
    13  Python                              0x000000010002251b instancemethod_call + 363
    14  Python                              0x000000010000fe02 PyObject_Call + 98
    15  Python                              0x00000001000bf7b7 PyEval_CallObjectWithKeywords + 87
    16  Python                              0x000000010002580e PyInstance_New + 126
    17  Python                              0x000000010000fe02 PyObject_Call + 98
    18  Python                              0x00000001000c4138 PyEval_EvalFrameEx + 14024
    19  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    20  Python                              0x00000001000c68ad PyEval_EvalFrameEx + 24125
    21  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    22  Python                              0x00000001000419e0 function_call + 176
    23  Python                              0x000000010000fe02 PyObject_Call + 98
    24  Python                              0x00000001000c1d2d PyEval_EvalFrameEx + 4797
    25  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    26  Python                              0x00000001000c68ad PyEval_EvalFrameEx + 24125
    27  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    28  Python                              0x00000001000c68ad PyEval_EvalFrameEx + 24125
    29  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    30  Python                              0x00000001000c68ad PyEval_EvalFrameEx + 24125
    31  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    32  Python                              0x00000001000419e0 function_call + 176
    33  Python                              0x000000010000fe02 PyObject_Call + 98
    34  Python                              0x000000010002251b instancemethod_call + 363
    35  Python                              0x000000010000fe02 PyObject_Call + 98
    36  Python                              0x00000001000bf7b7 PyEval_CallObjectWithKeywords + 87
    37  _core_.so                           0x000000010c06db02 _ZN12wxPyCallback12EventThunkerER7wxEvent + 258
    38  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e13c4 _ZN12wxEvtHandler23ProcessEventIfMatchesIdERK21wxEventTableEntryBasePS_R7wxEvent + 84
    39  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e237f _ZN12wxEvtHandler23SearchDynamicEventTableER7wxEvent + 111
    40  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e33ea _ZN12wxEvtHandler11TryHereOnlyER7wxEvent + 42
    41  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e345b _ZN12wxEvtHandler19ProcessEventLocallyER7wxEvent + 59
    42  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e3521 _ZN12wxEvtHandler12ProcessEventER7wxEvent + 161
    43  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4e2ed6 _ZN12wxEvtHandler18SafelyProcessEventER7wxEvent + 22
    44  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c72a7e0 _ZNK12wxWindowBase17HandleWindowEventER7wxEvent + 16
    45  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c57b8d9 _ZN9wxControl14ProcessCommandER14wxCommandEvent + 9
    46  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c544ac7 _ZN8wxButton16OSXHandleClickedEd + 71
    47  libsystem_trace.dylib               0x00007fff961c1cd7 _os_activity_initiate + 75
    48  AppKit                              0x00007fff8e375b71 -[NSApplication sendAction:to:from:] + 452
    49  AppKit                              0x00007fff8e375970 -[NSControl sendAction:to:] + 86
    50  AppKit                              0x00007fff8e54b86c __26-[NSCell _sendActionFrom:]_block_invoke + 131
    51  libsystem_trace.dylib               0x00007fff961c1cd7 _os_activity_initiate + 75
    52  AppKit                              0x00007fff8e3be509 -[NSCell _sendActionFrom:] + 144
    53  libsystem_trace.dylib               0x00007fff961c1cd7 _os_activity_initiate + 75
    54  AppKit                              0x00007fff8e3d9085 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2775
    55  AppKit                              0x00007fff8e3d82b9 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 491
    56  AppKit                              0x00007fff8e3d7899 -[NSControl mouseDown:] + 714
    57  AppKit                              0x00007fff8e8d5a18 -[NSWindow _reallySendEvent:] + 12721
    58  AppKit                              0x00007fff8e35c16e -[NSWindow sendEvent:] + 446
    59  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c5f7f5c -[wxNSWindow sendEvent:] + 140
    60  AppKit                              0x00007fff8e30e451 -[NSApplication sendEvent:] + 4183
    61  AppKit                              0x00007fff8e19a608 -[NSApplication run] + 711
    62  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c5e91c3 _ZN14wxGUIEventLoop5DoRunEv + 51
    63  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c4ad957 _ZN13wxCFEventLoop3RunEv + 55
    64  libwx_osx_cocoau-2.9.2.0.0.dylib    0x000000010c369828 _ZN16wxAppConsoleBase8MainLoopEv + 72
    65  _core_.so                           0x000000010c06690c _ZN7wxPyApp8MainLoopEv + 76
    66  _core_.so                           0x000000010c0b1b7f _wrap_PyApp_MainLoop + 79
    67  Python                              0x00000001000c7132 PyEval_EvalFrameEx + 26306
    68  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    69  Python                              0x00000001000419e0 function_call + 176
    70  Python                              0x000000010000fe02 PyObject_Call + 98
    71  Python                              0x000000010002251b instancemethod_call + 363
    72  Python                              0x000000010000fe02 PyObject_Call + 98
    73  Python                              0x00000001000c4138 PyEval_EvalFrameEx + 14024
    74  Python                              0x00000001000c76dd PyEval_EvalFrameEx + 27757
    75  Python                              0x00000001000c8fb9 PyEval_EvalCodeEx + 2137
    76  Python                              0x00000001000c90d6 PyEval_EvalCode + 54
    77  Python                              0x00000001000edc2e PyRun_FileExFlags + 174
    78  Python                              0x00000001000edeca PyRun_SimpleFileExFlags + 458
    79  Python                              0x000000010010563a Py_Main + 3770
    80  Python                              0x00000001000026c4 Python + 9924
    81  Python                              0x0000000100001c74 Python + 7284
)

Does anyone know something about this?

Looking forward to your help.

Ioyao Li
  • 3
  • 1

1 Answers1

0

wxPython will not work well with pylab/pyplot because they have both their own mainloop. Instead of pyplot you should let wxPython do the mainloop. See this stackoverflow post and that matplotlib example. You will notice in both examples that there is an self.axes/axes1 object (and no metioning of pyplot.

According to the NetworkX doc you can pass a matplotlib Axes object to the draw function, which should generate your plot (untested)

Community
  • 1
  • 1
nepix32
  • 3,012
  • 2
  • 14
  • 29
  • As long as the matplotlib backend is set to use wx then pyplot should work fine, at least in standalone applications. (Like usual, don't expect it to work from integrated environments or shells using another GUI toolkit.) If you `import wx` before any matplotlib modules are imported then it will usually select the WXagg backend by default, but I prefer to be more explicit about it and use `matplotlib.use('WXAgg')` at the beginning of the main module as shown in the linked SO post. – RobinDunn Feb 18 '15 at 16:31
  • @RobinDunn: Interesting! When calling `plt.show()` once, it works fine. My test app had a `wx.Dialog` and plot frame (generated on button click with `plt.show()`. However, when calling `plt.show()` a second time **without** closing the plot frame first, nothing will be redrawn (I have not found out how to kill the plot frame first). – nepix32 Feb 19 '15 at 12:37
  • Instead of `pyplot`, I've found my problem is something wrong with `nx.draw(G)`. My app crashs even without `plt.Show()`. – Ioyao Li Feb 21 '15 at 09:57