[PYTHON] Read the VTK file and display the color map with jupyter.

Introduction

What do you want to do

I want to display the VTK file of the 2D temperature simulation result with jupyter.

how?

Read the VTK file with Python, extract the coordinate data and temperature data, and display them with Matplotlib.

environment

OS: Windows 10 home edition

Tool: Anaconda(Python 3.7.4)

VTK file of simulation results

Using the finite element method free software Freefem ++, the thermal diffusion equation was solved and the resulting VTK file was created. The code of the heat diffusion equation used Heat.edp of the example. I will omit how to install and run Freefem ++. The used VTK file is stored in here.

Installing the Python VTK library

Install using conda.

conda install vtk

import relationship

Import the Numpy, Matplotlib, and VTK libraries required for operation.

import numpy as np
import maptlotlib.pyplot as plt
import vtk
from vtk.util import numpy_support
%matplotlib inline

Read VTK file

Since the VTK file was an unstructured grid, read it with Unstructured.

#File reading
filename = "heat_result.vtk"
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update()

Converting Temperature Cell Data to Point Data

The coordinate data of this VTK file is Point Data, and the temperature data is Cell Data. I want to have a one-to-one relationship between the coordinate data and the temperature data, so I convert them all to Point Data.

#Convert from cell data to point data
cell2point = vtk.vtkCellDataToPointData()
cell2point.SetInputData(reader.GetOutput())
cell2point.Update()

Extraction of coordinate and temperature data from the converted data

Read the VTK file and use vtk_support to extract the coordinate data and temperature data from the VTK data.

#Numpy coordinate and temperature data
coord = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPoints().GetData())
x = coord[:,0]
y = coord[:,1]
z = coord[:,2]#do not use. Since it is the result of a two-dimensional thermal diffusion equation, it is all 0.
#GetAbstractArray(0)Label, GetAbstractArray(1)Contains temperature data.
temperature = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPointData().GetAbstractArray(1))

Color map display in Matplotlib

Now that we're ready, all we have to do is view it in Matplotlib. Since it is unstructured data, the color map is displayed using trikontourf.

##Color map output
plt.tricontourf(x,y,temperature,levels=15,cmap="jet")
plt.colorbar()

coloarmap.png

All chords

https://github.com/matsxxx/show_vtk_on_jupyter

import numpy as np
import matplotlib.pyplot as plt
import vtk
from vtk.util import numpy_support
%matplotlib inline
#File reading
filename = "heat_result.vtk"
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(filename)
reader.Update()

#Convert from cell data to point data
cell2point = vtk.vtkCellDataToPointData()
cell2point.SetInputData(reader.GetOutput())
cell2point.Update()

#Numpy coordinate and temperature data
coord = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPoints().GetData())
x = coord[:,0]
y = coord[:,1]
z = coord[:,2]#do not use. Since it is the result of a two-dimensional thermal diffusion equation, it is all 0.
#GetAbstractArray(0)Label, GetAbstractArray(1)Contains temperature data.
temperature = numpy_support.vtk_to_numpy(cell2point.GetOutput().GetPointData().GetAbstractArray(1))

#Color map output
plt.tricontourf(x,y,temperature,levels=15,cmap="jet")
plt.colorbar()

reference

For the VTK class, the [class reference] of c ++ (https://vtk.org/doc/nightly/html/index.html) is open to the public. The structure and name of Python VTK is the same as the c ++ reference, so you can refer to it. The following sites were mainly referred to.

Recommended Posts

Read the VTK file and display the color map with jupyter.
[Python] Read the csv file and display the figure with matplotlib
Read the csv file with jupyter notebook and write the graph on top of it
Open an Excel file in Python and color the map of Japan
Read the file with python and delete the line breaks [Notes on reading the file]
Interactively display algebraic curves with Python and Jupyter
Read json file with Python, format it, and output json
Read and write csv file
Read the URL list with Robot Framework and surround the screenshots
Separate color images into RGB images and display them with OpenCV
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Read and write a file
matplotlib gallery and color map
How to make a command to read the configuration file with pyramid
Read a file in Python with a relative path from the program
Try to display google map and geospatial information authority map with python
Color the integration interval with matplotlib.pyplot
Follow the file hierarchy with fts
Run Pylint and read the results
[Python] Adjusted the color map standard
Simply view the Jupyter notebook file
Download the file deployed with appcfg.py
Let's display the map using Basemap
Open the file with the default app
Replace the directory name and the file name in the directory together with a Linux command.
Read and format a csv file mixed with comma tabs with Python pandas
I read the Sudachi synonym dictionary with Pandas and searched for synonyms
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
Image the pdf file and stamp all pages with confidential stamps (images).
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Read CSV file with Python and convert it to DataFrame as it is
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Implementation example] Read the file line by line with Cython (Python) from the last line
[Python] Calendar-style heat map (with holiday display)
Calculate and display standard weight with python
Specify the color in the matplotlib 2D map
Read CSV file with python (Download & parse CSV file)
Image display taken with the built-in ISIGHT
Check the existence of the file with python
Use Jupyter Lab and Jupyter Notebook with EC2
Jupyter with PYNQ and high-level synthesis with Polyphony
Read and write csv files with numpy
Read the file line by line in Python
Display the graph of tensorBoard on jupyter
Read the file line by line in Python
Set the xticklabels color individually with matplotlib
Read the file by specifying the character code.
Linking python and JavaScript with jupyter notebook
Display markers above the border with matplotlib
Try rewriting the file with the less command
Check the file size with du -sh *
Read a character data file with numpy
[Jupyter Notebook memo] Display kanji with matplotlib
[pandas] .csv file reading and display method
[python] Read html file and practice scraping
Download the file with PHP [Under construction]
Ramen map creation with Scrapy and Django
[Python] Read the specified line in the file
Read CSV and analyze with Pandas and Seaborn
Introducing the color map extension library palettable
[Automation] Read mail (msg file) with Python