Tuesday, May 23, 2017

Pycharm, Docker and Matplotlib on a Mac

May 23, 2017 Posted by Jason Irwin , , ,

The Issue

I'm playing with Tensorflow at the moment. I'm loving PyCharm's docker integration, but the drawback I've run into is plotting Matplotlib graphs. When attempting to plot a graph, the following error occurs:

RuntimeError: Invalid DISPLAY variable

The Workaround

The standard workaround is to save plots as images and open them from the local filesystem, like so:

This is a minor annoyance, at best.

The Solution

But there is another way! In order to get things working in PyCharm, three steps are necessary:

1. Install and start Socat

Kudos goes to this blog post for the tip and requisite install and setup instructions.

2. Make sure XQuartz is installed

This one should be fairly straightforward. Worth upgrading to latest and greatest if steps do not work out of the box...

3. Edit PyCharm run/debug configuration

PyCharm allows docker flags to be set via it's run/debug configuration settings. The DISPLAY environment variable needs to be set to the appropriate address (my local IP is but yours likely isn't...). Ideally I could use the hostname versus IP address but I need to dig some more.

The Outcome

If all goes well, when you plot your graph you'll end up with a nice little XQuartz window displaying said graph