[Matplotlib-users] Use of Pyplot in a GUI script

Eric Firing efiring at hawaii.edu
Wed May 18 12:13:07 EDT 2016


What happens if you put the following lines at the top:

import matplotlib
matplotlib.use('tkagg')

Eric

On 2016/05/18 5:44 AM, Eric's Gmail wrote:
> If I comment out the matplotib.pyplot call it runs fine. If i comment
> out the easygui call it also doesn’t crash. I can import Figure from
> matplotlib OK.
>
> Eric
>
>> On May 18, 2016, at 10:04 AM, Paul Hobson <pmhobson at gmail.com
>> <mailto:pmhobson at gmail.com>> wrote:
>>
>> Does that crash still occur if you don't import matplotlib? This
>> doesn't look at a matplotlib issue to me.
>>
>> On Wed, May 18, 2016 at 6:02 AM, Eric's Gmail <eric.shain at gmail.com
>> <mailto:eric.shain at gmail.com>> wrote:
>>
>>     Thomas,
>>
>>     This is a minimal example. In this case I’m not even plotting:
>>
>>     from easygui import fileopenbox
>>     import matplotlib.pyplot as plt
>>     import numpy as np
>>
>>     ft = ["JSON", ["*.jsn", "*.json"]]
>>     filename = fileopenbox(msg='Select JSON files',title='Import',
>>     filetypes=ft, multiple=False)
>>     print(filename)
>>     x = np.arange(10)
>>     y = x**2
>>
>>     Trying to run this from the terminal (OS X, 10.11.4, Python 3.5.1)
>>     yield the following:
>>
>>     Erics-MacBook-Air:QTtest ericshain$ python plottest.py
>>     2016-05-18 07:56:57.023 python[23268:3602317] -[NSApplication
>>     _setup:]: unrecognized selector sent to instance 0x109c3c500
>>     2016-05-18 07:56:57.038 python[23268:3602317] An uncaught
>>     exception was raised
>>     2016-05-18 07:56:57.039 python[23268:3602317] -[NSApplication
>>     _setup:]: unrecognized selector sent to instance 0x109c3c500
>>     2016-05-18 07:56:57.039 python[23268:3602317] (
>>     0   CoreFoundation                      0x00007fff8b1c94f2
>>     __exceptionPreprocess + 178
>>     1   libobjc.A.dylib                     0x00007fff8897af7e
>>     objc_exception_throw + 48
>>     2   CoreFoundation                      0x00007fff8b2331ad
>>     -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
>>     3   CoreFoundation                      0x00007fff8b139571
>>     ___forwarding___ + 1009
>>     4   CoreFoundation                      0x00007fff8b1390f8
>>     _CF_forwarding_prep_0 + 120
>>     5   libtk8.5.dylib                      0x000000010161c76d TkpInit
>>     + 472
>>     6   libtk8.5.dylib                      0x0000000101594f91
>>     Initialize + 1541
>>     7   _tkinter.cpython-35m-darwin.so
>>     <http://tkinter.cpython-35m-darwin.so/>      0x00000001003efded
>>     Tcl_AppInit + 77
>>     8   _tkinter.cpython-35m-darwin.so
>>     <http://tkinter.cpython-35m-darwin.so/>      0x00000001003ed7f9
>>     _tkinter_create + 889
>>     9   libpython3.5m.dylib                 0x00000001000679d9
>>     PyCFunction_Call + 233
>>     10  libpython3.5m.dylib                 0x00000001000f4cd7
>>     PyEval_EvalFrameEx + 36647
>>     11  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     12  libpython3.5m.dylib                 0x00000001000f5a07
>>     PyEval_EvalCodeEx + 71
>>     13  libpython3.5m.dylib                 0x00000001000426ba
>>     function_call + 186
>>     14  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     15  libpython3.5m.dylib                 0x000000010002b12c
>>     method_call + 140
>>     16  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     17  libpython3.5m.dylib                 0x0000000100080cc1
>>     slot_tp_init + 81
>>     18  libpython3.5m.dylib                 0x000000010007b964
>>     type_call + 212
>>     19  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     20  libpython3.5m.dylib                 0x00000001000edb9c
>>     PyEval_EvalFrameEx + 7660
>>     21  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     22  libpython3.5m.dylib                 0x00000001000f3c75
>>     PyEval_EvalFrameEx + 32453
>>     23  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     24  libpython3.5m.dylib                 0x00000001000f5a61
>>     PyEval_EvalCode + 81
>>     25  libpython3.5m.dylib                 0x00000001001247ee
>>     PyRun_FileExFlags + 206
>>     26  libpython3.5m.dylib                 0x0000000100124a8f
>>     PyRun_SimpleFileExFlags + 447
>>     27  libpython3.5m.dylib                 0x000000010013d497 Py_Main
>>     + 3479
>>     28  python                              0x0000000100000e92 main + 418
>>     29  python                              0x0000000100000cc4 start + 52
>>     30  ???                                 0x0000000000000002 0x0 + 2
>>     )
>>     2016-05-18 07:56:57.039 python[23268:3602317] *** Terminating app
>>     due to uncaught exception 'NSInvalidArgumentException', reason:
>>     '-[NSApplication _setup:]: unrecognized selector sent to instance
>>     0x109c3c500'
>>     *** First throw call stack:
>>     (
>>     0   CoreFoundation                      0x00007fff8b1c94f2
>>     __exceptionPreprocess + 178
>>     1   libobjc.A.dylib                     0x00007fff8897af7e
>>     objc_exception_throw + 48
>>     2   CoreFoundation                      0x00007fff8b2331ad
>>     -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
>>     3   CoreFoundation                      0x00007fff8b139571
>>     ___forwarding___ + 1009
>>     4   CoreFoundation                      0x00007fff8b1390f8
>>     _CF_forwarding_prep_0 + 120
>>     5   libtk8.5.dylib                      0x000000010161c76d TkpInit
>>     + 472
>>     6   libtk8.5.dylib                      0x0000000101594f91
>>     Initialize + 1541
>>     7   _tkinter.cpython-35m-darwin.so
>>     <http://tkinter.cpython-35m-darwin.so/>      0x00000001003efded
>>     Tcl_AppInit + 77
>>     8   _tkinter.cpython-35m-darwin.so
>>     <http://tkinter.cpython-35m-darwin.so/>      0x00000001003ed7f9
>>     _tkinter_create + 889
>>     9   libpython3.5m.dylib                 0x00000001000679d9
>>     PyCFunction_Call + 233
>>     10  libpython3.5m.dylib                 0x00000001000f4cd7
>>     PyEval_EvalFrameEx + 36647
>>     11  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     12  libpython3.5m.dylib                 0x00000001000f5a07
>>     PyEval_EvalCodeEx + 71
>>     13  libpython3.5m.dylib                 0x00000001000426ba
>>     function_call + 186
>>     14  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     15  libpython3.5m.dylib                 0x000000010002b12c
>>     method_call + 140
>>     16  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     17  libpython3.5m.dylib                 0x0000000100080cc1
>>     slot_tp_init + 81
>>     18  libpython3.5m.dylib                 0x000000010007b964
>>     type_call + 212
>>     19  libpython3.5m.dylib                 0x000000010000de63
>>     PyObject_Call + 99
>>     20  libpython3.5m.dylib                 0x00000001000edb9c
>>     PyEval_EvalFrameEx + 7660
>>     21  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     22  libpython3.5m.dylib                 0x00000001000f3c75
>>     PyEval_EvalFrameEx + 32453
>>     23  libpython3.5m.dylib                 0x00000001000f5900
>>     _PyEval_EvalCodeWithName + 2400
>>     24  libpython3.5m.dylib                 0x00000001000f5a61
>>     PyEval_EvalCode + 81
>>     25  libpython3.5m.dylib                 0x00000001001247ee
>>     PyRun_FileExFlags + 206
>>     26  libpython3.5m.dylib                 0x0000000100124a8f
>>     PyRun_SimpleFileExFlags + 447
>>     27  libpython3.5m.dylib                 0x000000010013d497 Py_Main
>>     + 3479
>>     28  python                              0x0000000100000e92 main + 418
>>     29  python                              0x0000000100000cc4 start + 52
>>     30  ???                                 0x0000000000000002 0x0 + 2
>>     )
>>     libc++abi.dylib: terminating with uncaught exception of type
>>     NSException
>>     Abort trap: 6
>>
>>
>>     Eric
>>
>>>     On May 18, 2016, at 7:52 AM, Thomas Caswell <tcaswell at gmail.com
>>>     <mailto:tcaswell at gmail.com>> wrote:
>>>
>>>     The crashes are likely due to conflicts between the multiple gui
>>>     event loops, how ever with out a minimal example it is hard to say.
>>>
>>>     pyplot functions are mostly auto-generated and have the form
>>>
>>>     def foo(*args, **kwargs):
>>>         retrun gca().foo(*args, **kwargs)
>>>
>>>     so anything you can do in pyplot you can do via the OO api.  Just
>>>     take a look at the pyplot source for the functions you want to
>>>     use and replace `gca` with an Axes object or `gcf` with a Figure
>>>     object.
>>>
>>>     Full Axes api docs: http://matplotlib.org/api/axes_api.html
>>>
>>>     Tom
>>>
>>>     On Wed, May 18, 2016 at 8:42 AM Eric's Gmail
>>>     <eric.shain at gmail.com <mailto:eric.shain at gmail.com>> wrote:
>>>
>>>         Ben,
>>>
>>>         I don’t really want a GUI application it this case. What I’m
>>>         usually wanting is run pyplot with a file open dialog and a
>>>         file save dialog. The file dialogs are important since I’m
>>>         importing lots of json files and it would be a mess to try to
>>>         type lots of long file names. I’ve tried several options
>>>         including easygui, Tk and pyqt4. In theses cases, I can run
>>>         them so long as I’m in an ipython console. However, as soon
>>>         as I try to run from the terminal, I get a crash. I’ve
>>>         managed by avoiding pyplot, but it is much nicer than using
>>>         matplotlib without it.
>>>
>>>         Eric
>>>
>>>>         On May 16, 2016, at 1:46 PM, Benjamin Root
>>>>         <ben.v.root at gmail.com <mailto:ben.v.root at gmail.com>> wrote:
>>>>
>>>>         In general, yes, you should avoid using pyplot when
>>>>         embedding. However, that rule actually only needs to be
>>>>         strictly followed when you are embedding the matplotlib's
>>>>         canvas into your own instantiated Window object. If you are
>>>>         embedding GUI elements into a matplotlib figure window, you
>>>>         actually can still utilize pyplot (but very carefully).
>>>>
>>>>         I suggest reading up my book:
>>>>         http://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843/
>>>>
>>>>         Cheers!
>>>>         Ben Root
>>>>
>>>>
>>>>         On Tue, Apr 26, 2016 at 5:28 PM, Eric <eric.shain at gmail.com
>>>>         <mailto:eric.shain at gmail.com>> wrote:
>>>>
>>>>             I've read that when writing a GUI application to avoid
>>>>             using pyplot. I
>>>>             experienced this when I wrote a python script that
>>>>             called the tkinter file
>>>>             dialog and then processed and plotted data. It would run
>>>>             when launched in
>>>>             Ipython but would crash when run in the terminal. It ran
>>>>             in terminal when I
>>>>             eliminated the file dialog and the file dialog code ran
>>>>             without the
>>>>             plotting. I rewrote the script using pyqt4, and avoided
>>>>             using pyplot using
>>>>             matplotlib figure instead. First question is is it true
>>>>             that I should avoid
>>>>             any use of pyplot in a gui app? Second is that I find it
>>>>             hard to do things
>>>>             in matplotlib that are easy in pyplot like drawing
>>>>             horizontal or vertical
>>>>             lines and such. Is there documentation and code examples
>>>>             using the
>>>>             matplotlib without pyplot?
>>>>
>>>>             Thanks, Eric
>>>>
>>>>
>>>>
>>>>             --
>>>>             View this message in context:
>>>>             http://matplotlib.1069221.n5.nabble.com/Use-of-Pyplot-in-a-GUI-script-tp47030.html
>>>>             Sent from the matplotlib - users mailing list archive at
>>>>             Nabble.com <http://nabble.com/>.
>>>>             _______________________________________________
>>>>             Matplotlib-users mailing list
>>>>             Matplotlib-users at python.org
>>>>             <mailto:Matplotlib-users at python.org>
>>>>             https://mail.python.org/mailman/listinfo/matplotlib-users
>>>>
>>>>
>>>
>>>         _______________________________________________
>>>         Matplotlib-users mailing list
>>>         Matplotlib-users at python.org <mailto:Matplotlib-users at python.org>
>>>         https://mail.python.org/mailman/listinfo/matplotlib-users
>>>
>>
>>
>>     _______________________________________________
>>     Matplotlib-users mailing list
>>     Matplotlib-users at python.org <mailto:Matplotlib-users at python.org>
>>     https://mail.python.org/mailman/listinfo/matplotlib-users
>>
>>
>
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>



More information about the Matplotlib-users mailing list