Plotting with Matplotlib

Hi everyone,

I’m using FEniCS 2019.1.0 and I’m trying to plot the solution of the Poisson equation using Matplotlib, instead of the built-in FEniCS method. The following lecture plotting, published in 2017, shows an example in which the Cahn-Hilliard tutorial is modified with the following lines

from matplotlib import pyplot
parameters [ " plotting_backend " ] = " matplotlib "

and the output is what one would expect -
plt_plot

Conversely, if I try adding the same lines to my program (again, using FEniCS 2019.1.0), I get the following error message printed to the console:


  File ~/anaconda3/envs/fenicsproject/lib/python3.12/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/cronin_group_research/Active-Particles/chemotaxis/autochemotaxis/single_particle/apply_exact_delta/untitled0.py:31
    fe.parameters ["plotting_backend"] = "matplotlib"

RuntimeError: Parameter plotting_backend not found in Parameters object

Is there any way I can make this work? Find below the program I’m trying to run

import fenics as fe
import mshr as msh
from matplotlib import pyplot

fe.parameters["plotting_backend"] = "matplotlib"
domain = msh.Circle(fe.Point(0,0), 1)
mesh = msh.generate_mesh(domain, 64)
V = fe.FunctionSpace(mesh, "P", 1)

u_D = fe.Expression("0", degree = 2)

def boundary(x, on_boundary):
    return on_boundary
bc = fe.DirichletBC(V, u_D, boundary)

u = fe.TrialFunction(V)
v = fe.TestFunction(V)
f = fe.Constant(0)

a = fe.dot( fe.grad(u), fe.grad(v) )*fe.dx
L = f*v*fe.dx

u = fe.Function(V)

assembler = fe.SystemAssembler(a, L, bc)
solver = fe.LUSolver("mumps")
A = fe.Matrix()
solver.set_operator(A)
assembler.assemble(A)

b = fe.assemble(L)
delta = fe.PointSource(V, fe.Point(0,0), 1)
delta.apply(b)
bc.apply(b)
fe.solve(A, u.vector(), b)

p = fe.plot(u)
p.set_cmap("viridis")

You are using a code originally prepared for FEniCS 2017 (or earlier) with FEniCS 2019, and there may have been some changes between the two versions. The first one you see is that the parameter plotting_backend was removed: simply drop it, FEniCS 2019 should default to matplotlib anyways.

1 Like