# How to display images and plots from ExternalEvaluator?

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

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()


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)!

• I feel like this was asked before but I can't find it... – M.R. Aug 15 '18 at 17:11
• 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. – b3m2a1 Aug 25 '18 at 4:02
• @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. – M.R. Aug 27 '18 at 17:37
• what do you mean it freezes? I thought it was throwing a NameError. – b3m2a1 Aug 27 '18 at 18:31
• 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. – b3m2a1 Aug 27 '18 at 19:28

import matplotlib