# 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
y_ = x
random.seed(x_)
rx = 2 * random.random() - 1
values = 0.5 * (1 + tanh(2 / wint * (y_ - L - 0.5e-4 * L * rx)))
values = 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 > (- DOLFIN_EPS) and on_boundary

def map(self, x, y):
y = x - L
y = x

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())
plt.show()

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

Here is the plot result:

Here is the paraview result:

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
y_ = x
random.seed(x_)
rx = 2 * random.random() - 1
values = 0.5 * (1 + tanh(2 / wint * (y_ - L - 0.5e-4 * L * rx)))
values = 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)
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())
``````
2 Likes

Thank you and it works! 