This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Fenics 2016.2.0 problem with pyplot

+6 votes

In Fenics 2016.1.0 I was using pyplot and could see on my screen the plots using

plt.show(block=False)

Today I moved to 2016.2.0 and it seems the default backend is now WebAgg, so I receive the message

To view figure, visit http://127.0.0.1:8000

Unfortunately this does not work, as the browser can’t establish a connection to this adress.

Also, even if it would work, I would prefer to use another backend because the WebAgg server is interrupting the progress of the running code. I tried to use

pyplot.switch_backend('GTKAgg')

and many other backend, but it seems they require pygtk, which is not installed. In fact pygtk is installed on my computer, so this has something to do with the container I guess? Now I am not sure what to do, can anyone help or give some information?

asked Feb 1, 2017 by Antoine FEniCS Novice (810 points)

I have the same problem in Fenics 2016.2. For example, I run

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])

and then get the message:

To view figure, visit http://127.0.0.1:8000
Press Ctrl+C to stop WebAgg server

But when I go to that address, I get

ERR_CONNECTION_REFUSED

If I comment on my comment, will this issue be bumped back to the top of the main Q/A page?

1 Answer

+2 votes

If you are running FEniCS inside a Docker container the backends using Qt won't be able to open a window in your host OS, hence the need for a "remote" solution using a web server inside the container. So you need to make port 8000 of the docker container accessible from the host OS. You can do this when you start the container with e.g.

      docker run -it \
      -v $(pwd):/home/fenics/shared \
      -p 127.0.0.1:8000:8000 \
      --name fenics \
      --hostname fenics \
      fenics

You can add as many ports as you wish with more lines like:

      -p 127.0.0.1:8888:8888 \

You can use that one to access ipython notebooks started in the container, which is by the way how I usually work with FEniCS. Then you can inline plots with %matplotlib inline.

answered Mar 1, 2017 by mdbenito FEniCS User (4,530 points)
edited Mar 2, 2017 by mdbenito

Thank you, adding the line

-p 127.0.0.1:8000:8000

when starting the container indeed works for the first plot. Then the code is paused and I receive the message

Press Ctrl+C to stop WebAgg server

and after Ctrl+C, the code keeps running but for the second plot I get the error:

File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py",
line 752, in start
raise RuntimeError("IOLoop is already running")

Anyway, maybe I just need to learn how to deal with WebAgg now.
So it seems that with the docker approach to FEniCS, the use of interactive plotting with Matplotlib is lost. I think it is a pity because it is really useful for debugging. If someone knows an alternative I'd be glad to hear it. I'll post here if I find a way.

I use iPython notebooks and inline plotting inside them (so instead you need to make port 8888 accessible from the host as mentioned). You have matplotlib for 2D and x3dom for 3D:

http://fenics.readthedocs.io/projects/containers/en/latest/jupyter.html#plotting

Here is an example:

http://fenics.readthedocs.io/projects/containers/en/latest/_downloads/jupyter-fenics-plotting-example.html

In iPython there's also the magic %matplotlib notebook, but it doesn't seem to work with the version bundled in the docker containers. It should be possible to derive a new container from those though.

Very helpful thanks, I will try this.

...