1 of 19

How to plot� unstructured mesh file�on Jupyter Notebook

@tkoyama010

2 of 19

Who am I ? About @tkoyama010

3 of 19

Introduction

Sorry it is hard to plot by

I need to plot

Can you plot me in Python?

4 of 19

2 ways of plotting mesh

matplotlib-3d

powered by

powered by

5 of 19

About Pyvista

import pyvista as pv

from pyvista import examples

mesh = examples.download_bunny_coarse()

cpos = [(0.2, 0.3, 0.9), (0, 0, 0), (0, 1, 0)]

mesh.plot(cpos=cpos, show_edges=True, color=True)

  • “VTK for humans”: a high-level API to the Visualization Toolkit (VTK)
  • mesh data structures and filtering methods for spatial datasets
  • 3D plotting made simple and built for large/complex data geometries

6 of 19

About Pyvista

7 of 19

About Pyvista

I can plot inline !

8 of 19

Background setting of Xvfb

#!/bin/bash

export PYVISTA_OFF_SCREEN=true

export PYVISTA_USE_PANEL=true

set -x

export DISPLAY=:99.0

which Xvfb

Xvfb :99 -screen 0 1024x768x24 > \

/dev/null 2>&1 &

sleep 3

set +x

exec "$@"

9 of 19

Development of PYVISTA_VIRTUAL_DISPLAY

#!/bin/bash

export PYVISTA_OFF_SCREEN=true

export PYVISTA_USE_PANEL=true

export PYVISTA_VIRTUAL_DISPLAY=true

from pyvirtualdisplay import Display

disp = Display().start()

# display is active

disp.stop()

# display is stopped

Python wrapper for Xvfb, Xephyr and Xvnc

10 of 19

About meshio

>>> import meshio

>>> mesh = meshio.read("bynny.obj")

>>> mesh.points

array([[-0.00341018, 0.13031957, 0.02175437],

[-0.08171916, 0.15250145, 0.02965609],

[-0.03054348, 0.12477885, 0.00109834],

...,

[-0.01389549, 0.16787168, -0.02189723],

[-0.069413 , 0.15121847, -0.04453854],

[-0.0550398 , 0.0573097 , 0.0169909 ]])

>>> mesh.cells

[CellBlock(type='triangle', data=array([[1068, 1646, 1577],

[1057, 908, 938],

[ 420, 1175, 237],

...,

[1486, 1317, 2502],

[1317, 1319, 2502],

[1319, 2442, 2502]]))]

There are various mesh formats available for representing unstructured meshes. meshio can read and write all of the following and smoothly converts between them:

Abaqus, ANSYS msh, AVS-UCD, CGNS, DOLFIN XML, Exodus, FLAC3D, H5M, Kratos/MDPA, Medit, MED/Salome, Nastran (bulk data), Neuroglancer precomputed format, Gmsh (format versions 2.2, 4.0, and 4.1), OBJ, OFF, PERMAS, PLY, STL, Tecplot .dat, TetGen .node/.ele, SVG (2D only, output only), SU2, UGRID, VTK, VTU, WKT (TIN), XDMF.

11 of 19

matplotlib-3d

plotting 2D projection by

Experimental 3d axis for matplotlib

This experimental project is an attempt at providing a better and more versatile 3d axis for Matplotlib. The heart of the code is explained in this blog post: Custom 3D engine in Matplotlib.

Install

You can install by pip command.

pip install git+https://github.com/rougier/matplotlib-3d

12 of 19

matplotlib-3d

Experimental 3d axis for matplotlib

This experimental project is an attempt at providing a better and more versatile 3d axis for Matplotlib. The heart of the code is explained in this blog post: Custom 3D engine in Matplotlib.

Install

You can install by pip command.

pip install git+https://github.com/rougier/matplotlib-3d

import numpy as np

from mpl3d import glm

from mpl3d.mesh import Mesh

from mpl3d.camera import Camera

import meshio

if __name__ == "__main__":

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,4))

ax = fig.add_axes([0,0,1,1], xlim=[-1,+1], ylim=[-1,+1], aspect=1)

ax.axis("off")

camera = Camera("ortho", scale=2)

mesh = meshio.read("data/bunny.obj")

vertices = mesh.points

faces = mesh.cells[0].data

vertices = glm.fit_unit_cube(vertices)

mesh = Mesh(ax, camera.transform, vertices, faces,

cmap=plt.get_cmap("magma"), edgecolors=(0,0,0,0.25))

camera.connect(ax, mesh.update)

plt.savefig("bunny.png", dpi=600)

plt.show()

13 of 19

matplotlib-3d

Experimental 3d axis for matplotlib

This experimental project is an attempt at providing a better and more versatile 3d axis for Matplotlib. The heart of the code is explained in this blog post: Custom 3D engine in Matplotlib.

Install

You can install by pip command.

pip install git+https://github.com/rougier/matplotlib-3d

14 of 19

matplotlib-3d

Experimental 3d axis for matplotlib

This experimental project is an attempt at providing a better and more versatile 3d axis for Matplotlib. The heart of the code is explained in this blog post: Custom 3D engine in Matplotlib.

Install

You can install by pip command.

pip install git+https://github.com/rougier/matplotlib-3d

I can plot without

15 of 19

matplotlib-3d

Note that we cannot have a full 3d engine because we do not have a proper zbuffer that allows to test individual pixels. This means we need to sort our points/lines/triangles in order to draw them from back to front. Most of the time, this does the trick but there exist some situations where it is impossible to avoid problems. For example, consider two triangles that intersect each other. In in such a case, we have to decide arbitrarily which triangle will be drawn on top of the other.

WARNING

Experimental 3d axis for matplotlib

This experimental project is an attempt at providing a better and more versatile 3d axis for Matplotlib. The heart of the code is explained in this blog post: Custom 3D engine in Matplotlib.

Install

You can install by pip command.

pip install git+https://github.com/rougier/matplotlib-3d

16 of 19

Publishing mesh to book using nbsphinx

Now I am in PDF

17 of 19

Conclusion

We can plot unstructured 3d mesh by using pyvista (best practice now)

We can plot unstructured 3d mesh by using matplotlib-3d (experimental)

We can generate book with unstructured 3d mesh by using nbsphinx.

Special thanks to jupyter & pyvista & matplotlib-3d & meshio & sphinx authors

18 of 19

Do you have a question?

If not, I can do demo.

19 of 19

Translation project of Pyvista