This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Plot a 3D function in 2D

+1 vote

Is it possible to plot a 3D function in 2D?

I can set the scale to 0.
But if I rotate the view it gets twisted.

 plot(phi, title='phi',interactive=True, scale=0.0) 

I would like to view it in aerial perspective.

Anyone has a suggestion?

closed with the note: Thank you both for your answers. Can be closed
asked May 25, 2016 by Whistler FEniCS Novice (460 points)
closed May 25, 2016 by Whistler

2 Answers

+1 vote
 
Best answer

You should consider exporting to vtu and using paraview or similar software. The built-in plotting functionality is mostly intended for debugging purposes.

fid = File('phi.vtu')
fid << phi
answered May 25, 2016 by KristianE FEniCS Expert (12,900 points)
selected May 25, 2016 by Whistler

I am using

file = File("phi.pvd", "compressed")
file << phi

and view the plots via paraview.

+1 vote

What I frequently do is interpolate a 2D slice of the Function on a 2D mesh and plot that.
At least for debugging/understanding the solutions I find this much more convenient than using an additional software pipeline.

I wrote a helper function for this:

def plot_cross(u, mesh2D, title="", axis=1):
   # create Expression to evaluate u on a hyperplane
   ucross = uCross(u=u, axis=axis)
   # interpolate u onto the 2D mesh
   V = dolfin.FunctionSpace(mesh2D, "CG", 1)
   u2D = dolfin.Function(V)
   u2D.interpolate(ucross)
   dolfin.plot(u2D, title=title)

class uCross(dolfin.Expression):
    def __init__(self, u, axis=1):
        self.u = u
        self.i = axis
        dolfin.Expression.__init__(self)
    def eval(self, value, x):
        y = list(x)
        y.insert(self.i, 0.)
        value[0] = self.u(y)

This code is limited in that it can only plot on crosssections which lie precisely on the x-y, x-z or y-z planes. (the "axis=1" default means that x-z is plotted by default, i.e. the direction with index 1, which is x1, is left out.)
But I'm sure it can give you a general idea how to implement more general crossectional plots.

Cheers, Gregor

answered May 25, 2016 by Gregor Mitscha-Baude FEniCS User (2,280 points)

This technique also facilitates export to vector graphics via pylab' s tricontourf function. Paraview does not really have this option.

fenicstools is convenient for plotting slices:
https://github.com/mikaem/fenicstools/wiki

...