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

When I try to solve the cahn hilliard problem, I visualize the initial value, in which the result of plot function drawing is very different from that of paraview drawing. So I’d like to know what caused this difference.
Here is my code:

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,


# Sub domain for Periodic boundary condition
class PeriodicBoundaryX(SubDomain):
    def inside(self, x, on_boundary):
        return DOLFIN_EPS > x[0] > (- DOLFIN_EPS) and on_boundary

    def map(self, x, y):
        y[0] = x[0] - L
        y[1] = x[1]



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, P1 * P1)
du = TrialFunction(ME)
q, v = TestFunctions(ME)
u = Function(ME)  # current solution
u0 = Function(ME)  # solution from previous converged step
dc, dmu = split(du)
c, mu = split(u)
c0, mu0 = split(u0)

u_init = InitialConditions(degree=1)
u.interpolate(u_init)
u0.interpolate(u_init)

plot(u.split()[0])
plt.show()

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

Here is the plot result:

Here is the paraview result:

Thank you for your answers!

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

Thank you and it works!
image