Plot figure wrong color in white in python

nx = ny = 20
mesh = UnitSquareMesh(nx, ny)

F = FunctionSpace(mesh, ‘P’, 1)
k = Function(F)

k_init = Expression(‘abs(x[0]-0.5)<l1 && x[1]<l2 ?1:0’, degree=2, l1=0.2,l2=0.4)
k.interpolate(k_init)
plt.colorbar(plot(k))

This snippet produce the following image:
download

a white region is shown, at which the value of function being plotted should be 1.
However, I also evaluate the value in that region, which is 1. Besides, if I save it as xdmf and read it by paraview, everything is fine.

The following is my import if that matters,

from future import print_function
from fenics import *
from dolfin import *
from mshr import *
import numpy as np
from tqdm import tqdm
import shutil
import matplotlib.pyplot as plt
%matplotlib inline

Besides, I am using docker installation of fenics,

DOLFIN_VERSION=2018.1.0.post1
PYPI_FENICS_VERSION=>=2018.1.0,<2018.2.0

Can you reproduce this result?
Any hint? What’s the suggested way in Fenics to do some simple visualization?

Hi, I can reproduce this error and suspect this is a bug. For workaround consider

from dolfin import *
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

nx = ny = 20
mesh = UnitSquareMesh(nx, ny)

F = FunctionSpace(mesh, 'P', 1)
k = Function(F)

k_init = Expression('abs(x[0]-0.5)<l1 && x[1]<l2 ?1:0', degree=2, l1=0.2,l2=0.4)
k.interpolate(k_init)

x, y = mesh.coordinates().T
triangles = mesh.cells()
triang = mtri.Triangulation(x, y, triangles)

z = k.compute_vertex_values()

plt.tricontourf(triang, z)
1 Like

You can use vtkplotter:

from dolfin import *

nx = ny = 20
mesh = UnitSquareMesh(nx, ny)

F = FunctionSpace(mesh, 'P', 1)
k = Function(F)

k_init = Expression('abs(x[0]-0.5)<l1 && x[1]<l2 ?1:0', degree=2, l1=0.2,l2=0.4)
k.interpolate(k_init)

from vtkplotter.dolfin import plot
plot(k, style=1, lc='w')

1 Like

This seems great, but can it plot in jupyter notebook? I try, but it didn’tshow figure.

…I just tried and it seems to work for me:

you need to install k3d:
pip install k3d

1 Like

Oh, this is great. I was using jupyter lab, which only show:

Plot(antialias=3, axes=[‘x’, ‘y’, ‘z’], background_color=16777215, camera=[0.5141421356237309, 0.5141421356237…

Thanks! this works in jupyter notebook.

1 Like

The original problem can easily be solved by changing the plot command to

plt.colorbar(plot(k, extend='max'))

These white regions frequently occur where functions are close to discontinuous. I assume that the large gradients will cause some kind of overshoots within the interpolation scheme. Interpolated function values that exceed the largest vertex values within a plot are not filled by default and thus remain white. The above keyword argument changes this behaviour.

Thanks, your code works. But the white region inside is continuous, discontinuity occurs when crossing the boundary.