# A VTK python module for analysis and simulation

**URL:** https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204
**Category:** Announcements
**Created:** [February 4, 2019, 11:52am UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204 "2019-02-04T11:52:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 4, 2019, 11:52am UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/1 "2019-02-04T11:52:06Z")

</div>

Dear vtk-ers,  
I would like to advertise/announce on this new platform a [python module](https://github.com/marcomusy/vtkplotter), developed under the support of EMBL (European Molecular Biology Laboratory), which may turn useful to the general VTK community of users.  
It is aimed at scientific visualization and research by creating an API which simplifies most common operations, without hiding access to the native vtk objects.

It currently includes more than 100 example scripts _(scroll down to see thumbnails)_ :  
[examples/basic](https://github.com/marcomusy/vtkplotter/blob/master/examples/basic)  
[examples/advanced](https://github.com/marcomusy/vtkplotter/blob/master/examples/advanced)  
[examples/volumetric](https://github.com/marcomusy/vtkplotter/blob/master/examples/volumetric)  
[examples/simulations](https://github.com/marcomusy/vtkplotter/blob/master/examples/simulations)  
[examples/other](https://github.com/marcomusy/vtkplotter/blob/master/examples/other).  
to perform basic functions, some analysis tools, e.g. moving least squares for pointclouds, spherical harmonics expansion, morphing and a bunch of fancy simulations.

Documentation can be found here: [vtkplotter.embl.es](https://vtkplotter.embl.es)  
Users feedback is always very welcome.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [February 5, 2019, 9:21am UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/2 "2019-02-05T09:21:49Z")

</div>

Looks interesting.

---

<div class="post-metadata">

### Author: ![pieper](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pieper/32/17_2.png) [@pieper](https://discourse.vtk.org/u/pieper)
#### Post date: [February 5, 2019, 12:25pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/3 "2019-02-05T12:25:14Z")

</div>

Yes, thanks for pointing this out - looks like a cool project 👍

---

<div class="post-metadata">

### Author: ![gatti](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/eb9ed0/32.png) [@gatti](https://discourse.vtk.org/u/gatti)
#### Post date: [February 6, 2019, 10:34pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/4 "2019-02-06T22:34:32Z")

</div>

As a new VTK(python) user, this looks amazing. The examples, as well as the code within the modules, are definitely helpful in getting a hold of vtk.

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [February 7, 2019, 12:11am UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/5 "2019-02-07T00:11:59Z")

</div>

Very nice. I’d like to use some of your datasets for the . VTKExamples project:

[https://lorensen.github.io/VTKExamples/site/](https://lorensen.github.io/VTKExamples/site/)

Bill

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 7, 2019, 3:35pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/6 "2019-02-07T15:35:14Z")

</div>

Hi Bill,  
Thanks for your interest,  
please go ahead.  
M.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [February 7, 2019, 3:57pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/7 "2019-02-07T15:57:56Z")

</div>

I’m also going to solicit attention for an open-source Python package interfacing to VTK I’ve been contributing to: [`vtki`](https://vtkinterface.readthedocs.io/en/latest/). It sounds like we have similar goals between `vtki` and `vtkplotter`, so perhaps there may be room for interoperability!

With `vtki`, we directly wrap VTK’s Python objects providing a “pythonic” interface back to the original VTK objects to make the process of editing points and data straightforward, [make many common filters easily accessible](https://vtkinterface.readthedocs.io/en/latest/examples/quick/using-filters.html), and preserve the original VTK data structure such that the objects can be passed to any external routines that accept VTK data objects. Our goal with the [plotting routines](https://vtkinterface.readthedocs.io/en/latest/tools/plotting.html) is to have `matplotlib` similar/familiar syntax and allow very general control over all the plotting parameters as well as **provide some [nifty filtering/plotting tools for IPython](https://vtkinterface.readthedocs.io/en/latest/tools/ipy_tools.html)**.

```python
import vtki
import numpy as np

```

_Editing points on a `vtki` object:_

```python
data = vtki.PolyData(np.random.rand(100,3))
data.points[:, -1] = 3

```

_Editing data on a `vtki` object:_

```auto
data.point_arrays['stuff'] = np.random.rand(data.n_points)
data.cell_arrays['better stuff'] = np.random.rand(data.n_cells)

```

_Using a filter on a `vtki` object:_

```python
# Apply a clip filter
clipped = data.clip()
# Apply a threshold filter:
threshed = data.threshold(0.5)
# etc.

```

_Convenient plotting on a `vtki` object:_

```python
data.plot()

```

_Accessing the original VTK data object:_

```python
foo = data.GetPoints()
bar = data.GetPointData().GetArray(0)
oof = data.GetNumberOfCells()

```

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 10, 2019, 7:47pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/8 "2019-02-10T19:47:27Z")

</div>

Hi Bane,  
indeed they are similar projects which might eventually merge/converge/interact (?).  
For sure one important point was for me as well to combine the use of `vtkplotter` and `vtk` seamlessly in a program, mantaining the access the full range of `vtk` classes.

In [vtkplotter](https://github.com/marcomusy/vtkplotter) the above code would translate into:

```python
import vtkplotter 
from numpy.random import rand

pts, scals = rand(100,3), rand(100)

# Create directly the Actor(vtkActor):
actor = vtkplotter.Points(pts)

# Editing points:
pts[:, -1] = 3
actor.setPoints(pts)

# Apply some chain of filters/operations:
# (vtkCleanPolyData, vtkTriangleFilter, vtkThreshold, ...):
actor.clean().triangle().threshold(scals, 0.5).mirror().scale(2)

# Plotting:
actor.addScalarBar().show() 

# Accessing the original VTK data object:
data = actor.polydata() # vtkPolyData
foo = data.GetPoints()
bar = data.GetPointData().GetArray(0)
oof = data.GetNumberOfCells()

```

![bane](https://discourse.vtk.org/uploads/default/original/1X/3c967bfadd50d616f7bad06460556456eff725c2.png)

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [February 3, 2020, 9:36pm UTC](https://discourse.vtk.org/t/a-vtk-python-module-for-analysis-and-simulation/204/9 "2020-02-03T21:36:08Z")

</div>

Just wanted to share with you some new developments exploiting the powerful `vtkAssembly` class that, among other things, allows composing nice plots in a “matplotlib way”. E.g.

```python
import numpy as np, vtk
from vtkplotter import plot

x = np.linspace(0, 5, 10)

plt = plot(x, np.sin(x), '*t--', title='y=sin(x)')

plt.show()

print('is vtkAssembly:', isinstance(plt, vtk.vtkAssembly))

```

![](https://discourse.vtk.org/uploads/default/original/2X/d/da3facf52efb1601c673e89d34d87bf917b5c157.png)

```bash
is vtkAssembly: True

```

Here’s a small random [gallery](https://github.com/marcomusy/vtkplotter-examples/tree/master/vtkplotter_examples/pyplot) of examples:

| | | |
| --- | --- | --- |
| ![](https://discourse.vtk.org/uploads/default/original/2X/2/2d8a5f8a238aafabc28595df0b67d883959de85a.png)  
`plot2_errband` | ![](https://discourse.vtk.org/uploads/default/original/2X/4/451817c51fc45b57f12d1038cb84c73204e09f44.png)   
`histo_2D` | ![](https://discourse.vtk.org/uploads/default/original/2X/2/28a809466c6930427361426719874b4b20156e25.png)   
`histo_hexagonal` |
| ![](https://discourse.vtk.org/uploads/default/original/2X/a/a3274f66c29bbf44f48856df6f502109a795e7c2.png)   
`plot5_spheric` | ![](https://discourse.vtk.org/uploads/default/original/2X/b/b029108d562a3fba5a483e51e760bdc966a71a2e.png)  
`plot7_stream` | ![](https://discourse.vtk.org/uploads/default/original/2X/e/eb0a1d99753bc987bb09e7f80ca0111fd98f20fd.png)  
`histo_violin` |
| ![](https://discourse.vtk.org/uploads/default/original/2X/1/1eb2021742c6d54a8d5e846a4fd46ced17ae29e8.png)   
`plot3_pip` | ![](https://discourse.vtk.org/uploads/default/original/2X/7/7c687a004d7a2bfe12e0262dd1759a7b97312420.png)   
`histo_spheric` | ![](https://discourse.vtk.org/uploads/default/original/2X/7/70c93ff3740f1cb4302a568c12eb79cadf1dcf5b.png)   
`histo_polar` |
| ![](https://discourse.vtk.org/uploads/default/original/2X/3/3f314e0af7fdc81c81396a2442dbd8f5b721bfb0.png)   
`plot1_errbars` | ![](https://discourse.vtk.org/uploads/default/original/2X/c/cfe53c90651f776aaecc3f56af20008204fa484c.png)   
`plot4_fxy` | ![](https://discourse.vtk.org/uploads/default/original/2X/c/cd0cec2009a3844c61e120493d2bb929d1d0653d.png)  
`histo_1D` |
