# How to plot VectorFunctionSpace with ndim=2 over IntervalMesh

**URL:** <https://fenicsproject.discourse.group/t/how-to-plot-vectorfunctionspace-with-ndim-2-over-intervalmesh/5487>\
**Category:** Uncategorized\
**Created:** [April 1, 2021, 1:51pm UTC](https://fenicsproject.discourse.group/t/how-to-plot-vectorfunctionspace-with-ndim-2-over-intervalmesh/5487 "2021-04-01T13:51:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Galaz](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jose_galaz/32/2309_2.png) [@Jose\_Galaz](https://fenicsproject.discourse.group/u/Jose_Galaz)\
**Post date:** [April 1, 2021, 1:51pm UTC](https://fenicsproject.discourse.group/t/how-to-plot-vectorfunctionspace-with-ndim-2-over-intervalmesh/5487/1 "2021-04-01T13:51:51Z")

</div>

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!

```auto
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)

```

```auto
---------------------------------------------------------------------------
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.

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 1, 2021, 3:08pm UTC](https://fenicsproject.discourse.group/t/how-to-plot-vectorfunctionspace-with-ndim-2-over-intervalmesh/5487/2 "2021-04-01T15:08:09Z")

</div>

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:

> [@How to get the solution obtained by fenics as a vector?](https://fenicsproject.discourse.group/t/how-to-get-the-solution-obatained-by-fenics-as-a-vector/5377/7):
>
> Please make a minimal example, as your original question is not clear on what kind of vector you would like to use. What you can do is to get each of the components of a VectorFunctionSpace by collapsing each of its components. from dolfin import \* import numpy as np mesh = UnitSquareMesh(10, 10) V = VectorFunctionSpace(mesh, "CG", 1) u = Function(V) u.interpolate(Expression(("3\*x[0]", "2\*x[1]"), degree=1)) num\_dofs\_per\_component = int(V.dim()/V.num\_sub\_spaces()) num\_sub\_spaces = V.num\_sub\_spac…
