lpjiang97
lpjiang97

Reputation: 323

wxPython Matplotlib.pyplot Conflict?

I have met this pretty weird bug when using wxPython today. This simple code works fine:

    import wx

    app = wx.App()
    frame = wx.Frame(None, -1, 'simple.py')
    frame.Show()
    app.MainLoop()

However, once I add an import statement for matplotlib.pyplot at top:

    import matplotlib.pyplot as plt

    # then same code as above...

the simple window does not show up anymore (without errors, program halts). Anyone know what the problem is?

Env: macOS High Sierra, Python 3.6.3, wxPython 4.0.0b2, matplotlib 2.1.0

Thanks!

Upvotes: 0

Views: 527

Answers (2)

Travis Thompson
Travis Thompson

Reputation: 61

I had this exact same problem. It only happens on macOS for me. I can run the program fine in Windows, but not Mac. I tested different versions of matplotlib to no avail. I wanted to test against the "classic" version of wxpython but I couldn't get it to download correctly from SourceForge.

I got around it by importing matplotlib inside of my function that did the plotting. This ran without errors but also produced some informative warnings:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook/deprecation.py:107: MatplotlibDeprecationWarning: The WX backend is deprecated. It's untested and will be removed in Matplotlib 3.0. Use the WXAgg backend instead. See Matplotlib usage FAQ for more info on backends. Use WXAgg instead. warnings.warn(message, mplDeprecation, stacklevel=1)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_wx.py:319: wxPyDeprecationWarning: Call to deprecated item. Use DrawText instead. gfx_ctx.DrawRotatedText(s, x - xo, y - yo, rads)

So it looks like the problem is due to some wx deprecation inside of matplotlib.

Rolf of Saxony's answer also worked for me and did not produce the warnings.

Upvotes: 1

Rolf of Saxony
Rolf of Saxony

Reputation: 22438

I suspect that it is crashing because you don't have a specific library available for the backend, probably python3-tk Try:

from matplotlib import use
use('WXAgg')
from matplotlib import pyplot as plt
import wx

app = wx.App()
frame = wx.Frame(None, -1, 'simple.py')
frame.Show()
app.MainLoop()

Upvotes: 2

Related Questions