Plot directly to png file

9 weeks ago by
Val K.  
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.
Thank you all in advance.
Community: FEniCS Project
Can you post a minimal code snippet that reproduces your error so we can check ourselves?
written 9 weeks ago by klunkean  
I am new at the forum, I accidentally reply to your request as answer to the original question (below).
written 9 weeks ago by Val K.  
It's not possible to tell exactly without a code example, but do you maybe still have a in there? If you use plt.savefig() then you should leave out, otherwise you get an empty figure in your output file.
written 9 weeks ago by alexdiem  

2 Answers

9 weeks ago by
Val K.  
Thank you all for your interest! I present a small part of the code . For multiple dirichlet boundary conditions I want to check the deformation of reference mesh, due to the displacement u of the nodes that are free to move. So, I just want to save each time the result directly in png & not as a vtkfile.

up   =  CompiledSubDomain("near(x[1], side) && on_boundary", side = y1)  # upper facet 
down =  CompiledSubDomain("near(x[1], side) && on_boundary", side = y0 ) # lower facet 

# Boundary Conditions:
x = "0.0"
y = ["-0.3", "-0.5","-0.7","-1.0"]  

counter = 0
for i in y:
	counter +=1
	upper = Expression((x,i), degree=1)
	lower = Expression(("0.0", "0.0"), degree=1)

	bc_up = DirichletBC(V, upper, up)  		
	bc_down = DirichletBC(V, lower, down)  
	bcs = [bc_up,bc_down]

	# --- Apply the Boundary Conditions:
	[bc.apply(u.vector()) for bc in bcs]

	# --- Some code where u is updated --- #
	fig = plot(u, mode = "displacement",interactive= False)

9 weeks ago by
Check out this code. It works for me. You have to first manually assign the current figure using the gcf()  (get current figure) function.
from fenics import *
import matplotlib.pyplot as plt

mesh = UnitSquareMesh(4,4)
FE = FiniteElement('Lagrange', mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, FE)

u = Function(V)
u.interpolate(Expression('sin(x[0])', element = FE))
counter = 1

fig = plt.gcf()
Please login to add an answer/comment or follow this question.

Similar posts:
Search »