### Plot directly to png file

64
views
0
9 weeks ago by
I have already checked two proposed ways about saving a figure to png format. Unfortunately the way with plt.savefig(), does no work for me. The output file in png format that is generated does not show the mesh , it's just a blank figure.
I would like to iterate over a mesh, and for different displacement on boundary, plot the resulted mesh. However, neither plt.savefif() nor fig.write_png("mesh") work correctly.
In addition, in each iteration it is impossible to keep the figure closed, i.e. not to appear on screen.

If there is any other way to save a figure in .png format , and in addition not to show the figure on each iteration, I would be very grateful if you could share it with me.
Community: FEniCS Project

0
9 weeks ago by
If you don't want plot windows to be shown, but only save png images, you can use the Agg backend:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as p​lt
from dolfin import *

mesh = UnitSquareMesh(8, 8)

plot(mesh)
plt.savefig('fig.png')

This also works inside docker, for example.

You should post a minimal working example of your code, from your question it is not possible to guess the error.