9
$\begingroup$

I have a conda python 3.6 environment registered as the default ExternalEvaluator with numpy, matplotlib, cv2, and PIL installed:

enter image description here

I'd like to show images and plot things (like one does from ipython shell, jupyter or spyder), for instance:

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Plotting with Python inside Mathematica')
plt.show()

enter image description here

But this fails and no x-window pops up. Is there a simple fix?

Update:

Oddly enough, this works intermittently but instead of failing however, the window it creates (which is at first hidden) is a zombie window that I can't close (look at the red circle x in the top left corner - it is not clickable)!

enter image description here

$\endgroup$
  • 1
    $\begingroup$ I feel like this was asked before but I can't find it... $\endgroup$ – M.R. Aug 15 '18 at 17:11
  • $\begingroup$ What's the python error exactly? That might explain it. It might be on the ZeroMQ side at which point you'd need to work around that. $\endgroup$ – b3m2a1 Aug 25 '18 at 4:02
  • $\begingroup$ @b3m2a1 On a clean kernel it works, but after I close the window generated and try again it freezes when I run the above cell. $\endgroup$ – M.R. Aug 27 '18 at 17:37
  • $\begingroup$ what do you mean it freezes? I thought it was throwing a NameError. $\endgroup$ – b3m2a1 Aug 27 '18 at 18:31
  • 2
    $\begingroup$ If matplotlib uses tkinter (it might) then this could possibly be explained by a new as of 2015, obnoxious way that it handles window closure. The main loop doesn’t close even when a window is closed and instead looks for a .destroy call (if memory serves—this has bitten me often in the past). If this is the case it would obviously lock Mathematica. Python-first programs might be smart enough to get around this. On the other hand if this is the case I’d need to actually run some stuff to figure out how to get around it rather than speculating. I’ll dig when I can. $\endgroup$ – b3m2a1 Aug 27 '18 at 19:28
4
$\begingroup$

Adding the second line in the snippet below makes it work:

import matplotlib
matplotlib.use('TkAgg') # doesn't work without this
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('Plotting with Python inside Mathematica')
plt.show()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.