How to plot VectorFunctionSpace with ndim=2 over IntervalMesh

Hello

I’m trying to plot a vector-valued function (ndim=2) over an interval using VectorFunctionSpace and IntervalMesh but when I run plot I get an AttributeError: Vector length must match geometric dimension..

Can anyone give me a hint on why this happens and/or how to fix it?

I’m using fenics from this docker image: quay.io/fenicsproject/stable:current from a jupyter notebook cell in jupyter lab.

Thanks!

import fenics
import numpy  as np
import matplotlib.pyplot as plt
%matplotlib inline

Lx = 10
nx = 10

mesh = fenics.IntervalMesh(nx-1, 0, Lx)
W = fenics.VectorFunctionSpace(mesh, 'P', 1, dim=2)

f1 = fenics.Expression(('cos(x[0])', 'sin(x[0])'), element = W.ufl_element())

u = fenics.interpolate(f1, W)

fenics.plot(u)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-541afb7716b5> in <module>
----> 1 fenics.plot(u)

/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in plot(object, *args, **kwargs)
    436     # Plot
    437     if backend == "matplotlib":
--> 438         return _plot_matplotlib(object, mesh, kwargs)
    439     elif backend == "x3dom":
    440         return _plot_x3dom(object, kwargs)

/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in _plot_matplotlib(obj, mesh, kwargs)
    302 
    303     if isinstance(obj, cpp.function.Function):
--> 304         return mplot_function(ax, obj, **kwargs)
    305     elif isinstance(obj, cpp.function.Expression):
    306         return mplot_expression(ax, obj, mesh, **kwargs)

/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in mplot_function(ax, f, **kwargs)
    199         nv = mesh.num_vertices()
    200         if len(w0) != gdim * nv:
--> 201             raise AttributeError('Vector length must match geometric dimension.')
    202         X = mesh.coordinates()
    203         X = [X[:, i] for i in range(gdim)]

AttributeError: Vector length must match geometric dimension.

The built in plotting in dolfin is quite limited, and I would suggest you write the data to file and visualize with paraview, or create your own custom visualization extracting the data as explained in: