Some wrong with 'plot' function compared with 'paraview'

As suggested in Setting colorbar range on fenics plot
you can do the following:

import random
from numpy.ma import tanh
import numpy as np
from fenics import *
from matplotlib import pyplot as plt


class InitialConditions(UserExpression):
    def eval(self, values, x):
        x_ = x[0]
        y_ = x[1]
        random.seed(x_)
        rx = 2 * random.random() - 1
        values[0] = 0.5 * (1 + tanh(2 / wint * (y_ - L - 0.5e-4 * L * rx)))
        values[1] = 0.0

    def value_shape(self):
        return (2,)


L = 0.176
wint = L / 16
nx = 192
ny = 384

mesh = RectangleMesh(Point(0, 0), Point(L, 2 * L), nx, ny)
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
ME = FunctionSpace(mesh, MixedElement([P1, P1]))
u = Function(ME)  # current solution
u0 = Function(ME)  # solution from previous converged step

u_init = InitialConditions(degree=1)
u.interpolate(u_init)
u0.interpolate(u_init)
u_0 = u.split(deepcopy=True)[0]
c=plot(u_0, mode='color', vmin=min(u_0.vector().get_local()), vmax=max(u_0.vector().get_local()))
plt.colorbar(c)
plt.savefig("test.png")

file = File('Cahn-Hilliard.pvd')
file << (u.split()[0])
2 Likes