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

How to plot directly to file, without opening a window?

+10 votes

Dear all,

is there some way to make a plot with the plot() command without opening the plot window? I would like to use the ipython notebook for presenting the capabilities of fenics/dolfin in a class. In order to embed the plots in the notebook, I need to save to a file, for which I would like to use the following code:

from dolfin import *
mesh = UnitSquare(6, 4)
wiz = plot(mesh, interactive=False)
wiz.write_png("mesh")

however, a plot window opens, which is quite inconvenient during a lecture. Furthermore, the window cannot be closed (ubuntu 14.04, ipython notebook 1.2.1, fenics 1.3.0.1), but that is an unrelated problem (1).

asked May 13, 2014 by rouckas FEniCS Novice (280 points)

(Edit: Sorry, I hadn't realised the question was so old)
Doesn't %matplotlib inline work?

3 Answers

+6 votes
 
Best answer

Hi, this is a workaround which uses matplotlib. You might also be interested in this post
about upcoming svg rendering of meshes directly by dolfin.

import matplotlib.pyplot as plt
import matplotlib.tri as tri
from dolfin import *
import numpy as np

domain = Rectangle(-1, -1, 1, 1) - Circle(0, 0, 0.5)
mesh = Mesh(domain, 20)
n = mesh.num_vertices()
d = mesh.geometry().dim()

# Create the triangulation
mesh_coordinates = mesh.coordinates().reshape((n, d))
triangles = np.asarray([cell.entities(0) for cell in cells(mesh)])
triangulation = tri.Triangulation(mesh_coordinates[:, 0],
                                  mesh_coordinates[:, 1],
                                  triangles)

# Plot the mesh
plt.figure()
plt.triplot(triangulation)
plt.savefig('mesh.png')

# Create some function
V = FunctionSpace(mesh, 'CG', 1)
f_exp = Expression('sin(2*pi*(x[0]*x[0]+x[1]*x[1]))')
f = interpolate(f_exp, V)

# Get the z values as face colors for each triangle(midpoint)
plt.figure()
zfaces = np.asarray([f(cell.midpoint()) for cell in cells(mesh)])
plt.tripcolor(triangulation, facecolors=zfaces, edgecolors='k')
plt.savefig('f0.png')

# Get the z values for each vertex
plt.figure()
z = np.asarray([f(point) for point in mesh_coordinates])
plt.tripcolor(triangulation, z, edgecolors='k')
plt.savefig('f1.png')

# Comment to prevent pop-up
plt.show()

Finally, here is an ipython notebook with complete showcase.
mesh
f

answered May 13, 2014 by MiroK FEniCS Expert (80,920 points)
edited May 14, 2014 by MiroK

Thank you, this answers my question, although I was hoping for a simpler solution. Plotting meshes with matplotlib will be useful for me. However, is there some fundamental reason why "off-screen" rendering is not supported by plot()? Would it be difficult to implement?

I don't know the plot mechanism well enough to answer these questions. I suggest you ask
on the developers mailing list.

How can we use the above formulation for plotting vectors, I mean a function defined in a vector-function-space ? Like, displacement field for elasticity problem.
Is it possible?

It is possible. This answer is a good starting point.

+2 votes

Hello,

use that: (I guess it will work :p )

file = File('nameofprogr.pdv')
file << mesh
answered May 13, 2014 by Shizu FEniCS Novice (350 points)

Thank you for suggestion, but I don't see how it can be used to plot the mesh. (Other than saving the mesh to .pvd file and plotting with paraview, but that is not what I am after)

0 votes

Hi everyone, I know it is an old question but I had this issue and couldn't use matplotlib because I don't know how to put a colorbar on the plot (plt.colorbar() doesn't work). In case someone wants to store lots of png files, setting a constant key to the plots will keep only one plot open (the initial one) and repeatedly replace that one:

fig= plot(function, interactive = False, key = "0")
fig.write_png(filename)

It should also be possible to set a figure to "closed" with the "key_pressed" method, by setting "q" to force it closed. Regards!

answered Apr 18, 2017 by nabarnaf FEniCS User (2,940 points)
...