PyVista:一款Python的三维可视化软件

技术背景

三维可视化是一项在工业领域中非常重要的技术,而Python中最热门的可视化工具matplotlib和plotly,更加倾向于在数据领域的可视化,用于展现数据的结果。类似的还有百度的pyechart也相对美观,但是这些毕竟都是在数据层面的可视化,对于工业领域,比如一个地形,一个三维的期间等等,用这些工具来做可视化效果非常的不佳,因此我找到了pyvista这个工具,简单摸索了一下给大家做个引荐。

安装pyvista

因为pyvista及其依赖都是一些python库,这就使得我们可以用pip直接简单的安装,这里放几个可能用到的依赖的安装流程:

(base) dechin@ubuntu2004:~/projects$ python3 -m pip install vtk==9.0.20210612.dev0
Collecting vtk==9.0.20210612.dev0
  Downloading vtk-9.0.20210612.dev0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (80.9 MB)
     |████████████████████████████████| 80.9 MB 3.7 MB/s 
Requirement already satisfied: wslink>=0.1.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk==9.0.20210612.dev0) (0.2.0)
Requirement already satisfied: Twisted>=17.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk==9.0.20210612.dev0) (21.2.0)
Requirement already satisfied: matplotlib>=2.0.0 in /home/dechin/.local/lib/python3.8/site-packages (from vtk==9.0.20210612.dev0) (3.3.4)
Requirement already satisfied: autobahn>=17.7.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk==9.0.20210612.dev0) (21.3.1)
Requirement already satisfied: cryptography>=3.4.6 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk==9.0.20210612.dev0) (3.4.7)
Requirement already satisfied: txaio>=21.2.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk==9.0.20210612.dev0) (21.2.1)
Requirement already satisfied: hyperlink>=21.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk==9.0.20210612.dev0) (21.0.0)
Requirement already satisfied: cffi>=1.12 in /home/dechin/anaconda3/lib/python3.8/site-packages (from cryptography>=3.4.6->autobahn>=17.7.1->vtk==9.0.20210612.dev0) (1.14.5)
Requirement already satisfied: pycparser in /home/dechin/anaconda3/lib/python3.8/site-packages (from cffi>=1.12->cryptography>=3.4.6->autobahn>=17.7.1->vtk==9.0.20210612.dev0) (2.20)
Requirement already satisfied: idna>=2.5 in /home/dechin/.local/lib/python3.8/site-packages (from hyperlink>=21.0.0->autobahn>=17.7.1->vtk==9.0.20210612.dev0) (2.10)
Requirement already satisfied: python-dateutil>=2.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (2.8.1)
Requirement already satisfied: pillow>=6.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (8.2.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (1.3.1)
Requirement already satisfied: numpy>=1.15 in /home/dechin/anaconda3/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (1.20.2)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (2.4.7)
Requirement already satisfied: cycler>=0.10 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (0.10.0)
Requirement already satisfied: six in /home/dechin/.local/lib/python3.8/site-packages (from cycler>=0.10->matplotlib>=2.0.0->vtk==9.0.20210612.dev0) (1.16.0)
Requirement already satisfied: zope.interface>=4.4.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk==9.0.20210612.dev0) (5.4.0)
Requirement already satisfied: attrs>=19.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk==9.0.20210612.dev0) (21.2.0)
Requirement already satisfied: constantly>=15.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk==9.0.20210612.dev0) (15.1.0)
Requirement already satisfied: Automat>=0.8.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk==9.0.20210612.dev0) (20.2.0)
Requirement already satisfied: incremental>=16.10.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk==9.0.20210612.dev0) (21.3.0)
Requirement already satisfied: setuptools in /home/dechin/anaconda3/lib/python3.8/site-packages (from zope.interface>=4.4.2->Twisted>=17.5.0->vtk==9.0.20210612.dev0) (52.0.0.post20210125)
Installing collected packages: vtk
  Attempting uninstall: vtk
    Found existing installation: vtk 9.0.2
    Uninstalling vtk-9.0.2:
      Successfully uninstalled vtk-9.0.2
Successfully installed vtk-9.0.20210612.dev0

安装pyvista的时候最好加上一个国内的镜像源,否则有可能出现网络问题,其他的包不需要加镜像源:

(base) dechin@ubuntu2004:~/projects$ python3 -m pip install pyvista --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple
Looking in indexes: https://repo.huaweicloud.com/repository/pypi/simple
Collecting pyvista
  Downloading https://repo.huaweicloud.com/repository/pypi/packages/ec/00/292dc2f14247d74098806dcb9e1bb0c416b936c145ca9ab4e940b6f90d4f/pyvista-0.31.3-py3-none-any.whl (1.3 MB)
     |████████████████████████████████| 1.3 MB 2.7 MB/s 
Requirement already satisfied: imageio in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista) (2.9.0)
Requirement already satisfied: numpy in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista) (1.20.2)
Requirement already satisfied: pillow in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista) (8.2.0)
Collecting scooby>=0.5.1
  Downloading https://repo.huaweicloud.com/repository/pypi/packages/1b/99/db6d34bdc3f060d631f524c2f0fc4b1919cd3bf734c905fc1b25eb847ac2/scooby-0.5.7-py3-none-any.whl (13 kB)
Collecting transforms3d==0.3.1
  Downloading https://repo.huaweicloud.com/repository/pypi/packages/b5/f7/e85809168a548a854d7c1331560c27b4f5381698d29c12e57759192b2bc1/transforms3d-0.3.1.tar.gz (62 kB)
     |████████████████████████████████| 62 kB 2.2 MB/s 
Requirement already satisfied: vtk in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista) (9.0.20210612.dev0)
Requirement already satisfied: appdirs in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista) (1.4.4)
Collecting meshio<5.0,>=4.0.3
  Downloading https://repo.huaweicloud.com/repository/pypi/packages/bb/36/02702cfc5fdf19e6477ea2a78cac4a774a8da4c2cf9557f3ddfb33c74192/meshio-4.4.6-py3-none-any.whl (158 kB)
     |████████████████████████████████| 158 kB 2.7 MB/s 
Requirement already satisfied: autobahn>=17.7.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista) (21.3.1)
Requirement already satisfied: wslink>=0.1.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista) (0.2.0)
Requirement already satisfied: matplotlib>=2.0.0 in /home/dechin/.local/lib/python3.8/site-packages (from vtk->pyvista) (3.3.4)
Requirement already satisfied: Twisted>=17.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista) (21.2.0)
Requirement already satisfied: txaio>=21.2.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista) (21.2.1)
Requirement already satisfied: cryptography>=3.4.6 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista) (3.4.7)
Requirement already satisfied: hyperlink>=21.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista) (21.0.0)
Requirement already satisfied: cffi>=1.12 in /home/dechin/anaconda3/lib/python3.8/site-packages (from cryptography>=3.4.6->autobahn>=17.7.1->vtk->pyvista) (1.14.5)
Requirement already satisfied: pycparser in /home/dechin/anaconda3/lib/python3.8/site-packages (from cffi>=1.12->cryptography>=3.4.6->autobahn>=17.7.1->vtk->pyvista) (2.20)
Requirement already satisfied: idna>=2.5 in /home/dechin/.local/lib/python3.8/site-packages (from hyperlink>=21.0.0->autobahn>=17.7.1->vtk->pyvista) (2.10)
Requirement already satisfied: python-dateutil>=2.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista) (2.4.7)
Requirement already satisfied: cycler>=0.10 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista) (1.3.1)
Requirement already satisfied: six in /home/dechin/.local/lib/python3.8/site-packages (from cycler>=0.10->matplotlib>=2.0.0->vtk->pyvista) (1.16.0)
Requirement already satisfied: zope.interface>=4.4.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista) (5.4.0)
Requirement already satisfied: attrs>=19.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista) (21.2.0)
Requirement already satisfied: constantly>=15.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista) (15.1.0)
Requirement already satisfied: Automat>=0.8.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista) (20.2.0)
Requirement already satisfied: incremental>=16.10.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista) (21.3.0)
Requirement already satisfied: setuptools in /home/dechin/anaconda3/lib/python3.8/site-packages (from zope.interface>=4.4.2->Twisted>=17.5.0->vtk->pyvista) (52.0.0.post20210125)
Building wheels for collected packages: transforms3d
  Building wheel for transforms3d (setup.py) ... done
  Created wheel for transforms3d: filename=transforms3d-0.3.1-py3-none-any.whl size=59373 sha256=56293f06c7932f3c12d57476773220d4219d8a978257f84bf56f4a8be8cccb2b
  Stored in directory: /home/dechin/.cache/pip/wheels/45/80/2d/22eb03277c315a020a6fdb617cc1232ef4ddc04dc2f00b8a22
Successfully built transforms3d
Installing collected packages: transforms3d, scooby, meshio, pyvista
Successfully installed meshio-4.4.6 pyvista-0.31.3 scooby-0.5.7 transforms3d-0.3.1

另外还有这个三维可交互面板绘制的插件:

(base) dechin@ubuntu2004:~/projects$ python3 -m pip install ipygany
Collecting ipygany
  Using cached ipygany-0.5.0-py2.py3-none-any.whl (2.9 MB)
Requirement already satisfied: numpy in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipygany) (1.20.2)
Requirement already satisfied: traittypes in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipygany) (0.2.1)
Requirement already satisfied: ipywidgets>=7.6.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipygany) (7.6.3)
Requirement already satisfied: ipython>=4.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (7.22.0)
Requirement already satisfied: ipykernel>=4.5.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (5.3.4)
Requirement already satisfied: widgetsnbextension~=3.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (3.5.1)
Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (1.0.0)
Requirement already satisfied: traitlets>=4.3.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (5.0.5)
Requirement already satisfied: nbformat>=4.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipywidgets>=7.6.0->ipygany) (5.1.3)
Requirement already satisfied: jupyter-client in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets>=7.6.0->ipygany) (6.1.12)
Requirement already satisfied: tornado>=4.2 in /home/dechin/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets>=7.6.0->ipygany) (6.1)
Requirement already satisfied: backcall in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.2.0)
Requirement already satisfied: pygments in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (2.9.0)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (3.0.17)
Requirement already satisfied: decorator in /home/dechin/.local/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (4.4.2)
Requirement already satisfied: pexpect>4.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (4.8.0)
Requirement already satisfied: jedi>=0.16 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.17.2)
Requirement already satisfied: setuptools>=18.5 in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (52.0.0.post20210125)
Requirement already satisfied: pickleshare in /home/dechin/anaconda3/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.7.5)
Requirement already satisfied: parso<0.8.0,>=0.7.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.7.0)
Requirement already satisfied: jupyter-core in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (4.7.1)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (3.2.0)
Requirement already satisfied: ipython-genutils in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (0.2.0)
Requirement already satisfied: six>=1.11.0 in /home/dechin/.local/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (1.16.0)
Requirement already satisfied: pyrsistent>=0.14.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (0.17.3)
Requirement already satisfied: attrs>=17.4.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.6.0->ipygany) (21.2.0)
Requirement already satisfied: ptyprocess>=0.5 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.7.0)
Requirement already satisfied: wcwidth in /home/dechin/anaconda3/lib/python3.8/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets>=7.6.0->ipygany) (0.2.5)
Requirement already satisfied: notebook>=4.4.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (6.4.0)
Requirement already satisfied: pyzmq>=17 in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (20.0.0)
Requirement already satisfied: Send2Trash>=1.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (1.5.0)
Requirement already satisfied: terminado>=0.8.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.9.4)
Requirement already satisfied: argon2-cffi in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (20.1.0)
Requirement already satisfied: nbconvert in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (6.1.0)
Requirement already satisfied: jinja2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (3.0.1)
Requirement already satisfied: prometheus-client in /home/dechin/anaconda3/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.11.0)
Requirement already satisfied: python-dateutil>=2.1 in /home/dechin/.local/lib/python3.8/site-packages (from jupyter-client->ipykernel>=4.5.1->ipywidgets>=7.6.0->ipygany) (2.8.1)
Requirement already satisfied: cffi>=1.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (1.14.5)
Requirement already satisfied: pycparser in /home/dechin/anaconda3/lib/python3.8/site-packages (from cffi>=1.0.0->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (2.20)
Requirement already satisfied: MarkupSafe>=2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (2.0.1)
Requirement already satisfied: entrypoints>=0.2.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.3)
Requirement already satisfied: pandocfilters>=1.4.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (1.4.3)
Requirement already satisfied: testpath in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.5.0)
Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.5.3)
Requirement already satisfied: jupyterlab-pygments in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.1.2)
Requirement already satisfied: mistune<2,>=0.8.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.8.4)
Requirement already satisfied: defusedxml in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.7.1)
Requirement already satisfied: bleach in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (3.3.0)
Requirement already satisfied: nest-asyncio in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (1.5.1)
Requirement already satisfied: async-generator in /home/dechin/anaconda3/lib/python3.8/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (1.10)
Requirement already satisfied: webencodings in /home/dechin/anaconda3/lib/python3.8/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (0.5.1)
Requirement already satisfied: packaging in /home/dechin/anaconda3/lib/python3.8/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (20.9)
Requirement already satisfied: pyparsing>=2.0.2 in /home/dechin/.local/lib/python3.8/site-packages (from packaging->bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.6.0->ipygany) (2.4.7)
Installing collected packages: ipygany
Successfully installed ipygany-0.5.0

以及一个基于pyqt的一个弹出式绘图插件:

(base) dechin@ubuntu2004:~/projects$ python3 -m pip install pyvistaqt
Collecting pyvistaqt
  Downloading pyvistaqt-0.5.0-py3-none-any.whl (162 kB)
     |████████████████████████████████| 162 kB 1.1 MB/s 
Requirement already satisfied: QtPy>=1.9.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvistaqt) (1.9.0)
Requirement already satisfied: pyvista>=0.25.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvistaqt) (0.31.3)
Requirement already satisfied: pillow in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (8.2.0)
Requirement already satisfied: meshio<5.0,>=4.0.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (4.4.6)
Requirement already satisfied: scooby>=0.5.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (0.5.7)
Requirement already satisfied: imageio in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (2.9.0)
Requirement already satisfied: appdirs in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (1.4.4)
Requirement already satisfied: numpy in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (1.20.2)
Requirement already satisfied: vtk in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (9.0.20210612.dev0)
Requirement already satisfied: transforms3d==0.3.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from pyvista>=0.25.0->pyvistaqt) (0.3.1)
Requirement already satisfied: autobahn>=17.7.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista>=0.25.0->pyvistaqt) (21.3.1)
Requirement already satisfied: Twisted>=17.5.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista>=0.25.0->pyvistaqt) (21.2.0)
Requirement already satisfied: matplotlib>=2.0.0 in /home/dechin/.local/lib/python3.8/site-packages (from vtk->pyvista>=0.25.0->pyvistaqt) (3.3.4)
Requirement already satisfied: wslink>=0.1.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from vtk->pyvista>=0.25.0->pyvistaqt) (0.2.0)
Requirement already satisfied: hyperlink>=21.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (21.0.0)
Requirement already satisfied: cryptography>=3.4.6 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (3.4.7)
Requirement already satisfied: txaio>=21.2.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (21.2.1)
Requirement already satisfied: cffi>=1.12 in /home/dechin/anaconda3/lib/python3.8/site-packages (from cryptography>=3.4.6->autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (1.14.5)
Requirement already satisfied: pycparser in /home/dechin/anaconda3/lib/python3.8/site-packages (from cffi>=1.12->cryptography>=3.4.6->autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (2.20)
Requirement already satisfied: idna>=2.5 in /home/dechin/.local/lib/python3.8/site-packages (from hyperlink>=21.0.0->autobahn>=17.7.1->vtk->pyvista>=0.25.0->pyvistaqt) (2.10)
Requirement already satisfied: python-dateutil>=2.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista>=0.25.0->pyvistaqt) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista>=0.25.0->pyvistaqt) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista>=0.25.0->pyvistaqt) (1.3.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/dechin/.local/lib/python3.8/site-packages (from matplotlib>=2.0.0->vtk->pyvista>=0.25.0->pyvistaqt) (2.4.7)
Requirement already satisfied: six in /home/dechin/.local/lib/python3.8/site-packages (from cycler>=0.10->matplotlib>=2.0.0->vtk->pyvista>=0.25.0->pyvistaqt) (1.16.0)
Requirement already satisfied: Automat>=0.8.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (20.2.0)
Requirement already satisfied: attrs>=19.2.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (21.2.0)
Requirement already satisfied: constantly>=15.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (15.1.0)
Requirement already satisfied: zope.interface>=4.4.2 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (5.4.0)
Requirement already satisfied: incremental>=16.10.1 in /home/dechin/anaconda3/lib/python3.8/site-packages (from Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (21.3.0)
Requirement already satisfied: setuptools in /home/dechin/anaconda3/lib/python3.8/site-packages (from zope.interface>=4.4.2->Twisted>=17.5.0->vtk->pyvista>=0.25.0->pyvistaqt) (52.0.0.post20210125)
Installing collected packages: pyvistaqt
Successfully installed pyvistaqt-0.5.0

到这里为止,我们所需要的插件就基本上安装完成了。

案例测试

到这里为止我们就可以看下这个库的运行效果。

画单个球体

首先执行一个画球的简单案例,这里一般都是在jupyter notebook上实现的,pyvista对于jupyter notebook有较好的支持。

import pyvista as pv
sphere = pv.Sphere()
sphere.plot(jupyter_backend='static')

运行的效果如下:

pyvistaqt案例

这里是一个官方提供的弹出式窗口的绘图方案:

from threading import Thread
import time
import numpy as np
import pyvista as pv
import pyvistaqt as pvqt
from pyvista import examples


globe = examples.load_globe()
globe.point_arrays['scalars'] = np.random.rand(globe.n_points)
globe.set_active_scalars('scalars')


plotter = pvqt.BackgroundPlotter()
plotter.add_mesh(globe, lighting=False, show_edges=True, texture=True, scalars='scalars')
plotter.view_isometric()

# shrink globe in the background
def shrink():
    for i in range(50):
        globe.points *= 0.95
        # Update scalars
        globe.point_arrays['scalars'] = np.random.rand(globe.n_points)
        time.sleep(0.5)

thread = Thread(target=shrink)
thread.start()

执行后会弹出一个pyqt的窗口如下所示:

这个图其实是一个动态图,但是qt的这个方案似乎不能在界面上直接导出gif,这一点比较可惜。不过在pyvista的接口文档中,其实是包含导出gif视频和mp4视频的,相关接口可以参考:GIF生成示例MP4生成示例这两个案例。

多模块可视化

在pyvista里面可以用MultiBlock将相关的模块都集成起来,比如这个案例中集成了两个球体,其实使用的方法也很简单,就是把创建的对象归纳到一个列表中:

import pyvista as pv
sphere1 = pv.Sphere(radius=0.1, center=(0, 0, 0))
sphere2 = pv.Sphere(radius=0.1, center=(0, 0, 1))
data = [sphere1,sphere2]
blocks = pv.MultiBlock(data)
blocks.plot(jupyter_backend='static',show_axes=1)

生成的结果如下图所示:

需要注意的是,因为这里都还是静态图片,三维的视角不可以调整,因此如果坐标设置不当的话,有可能导致两个球体的视角刚好重合,看起来就只有一个球体。

多模块可视化耗时

基于上述的MultiBlock,我们可以很方便的生成一大堆的数据,但是这里可视化的速率也是我们不得不考虑的一个因素,所以这里我们尝试一个1000个球体的可视化,并输出时间:

import pyvista as pv
import numpy as np
import time
start_time=time.time()
data = [pv.Sphere(radius=0.01, center=(np.random.random(), np.random.random(), np.random.random())) for i in range(1000)]
blocks = pv.MultiBlock(data)
blocks.plot(jupyter_backend='static',show_axes=1)
end_time=time.time()
print ('The time cost of ploting {} spheres is: {}s'.format(len(data),end_time-start_time))

生成的结果如下:

经过过程统计,耗时将近10s。

The time cost of ploting 1000 spheres is: 9.896512746810913s

这其实是一个比较慢的速度,让人有点担忧啊,对于一些几十万的体系,那可视化时间不得冲一天去了,这还不包含中间的时间戳。

动态画板

这里要用到我们之前安装的一个插件ipygany,可以在jupyter notebook中显示出来一个动态的画板,这样我们就可以用鼠标去拖动这个三维图,可以看不同的视角,如下是一个简单的单球体案例:

import pyvista as pv
sphere = pv.Sphere()
plotter = pv.Plotter(notebook=True)
plotter.add_mesh(sphere)
plotter.show(jupyter_backend='ipygany')

可视化效果图大概这样,只有在jupyter notebook中才能看到效果:

最后补充一个自己写的简单案例,可以在面板上画两个不同位置的三维球,使用方法是直接将两个对象加起来,这个就是python中一些魔法函数的优势了,非常的人性化:

import pyvista as pv
plotter = pv.Plotter(notebook=True)
plotter.add_mesh(pv.Sphere(radius=0.3,center=(0,0,0))+pv.Sphere(radius=0.3,center=(0,0,1)))
plotter.show(jupyter_backend='ipygany')

可视化的效果大概如下:

那么到这里,需要将的基本用例就介绍完成,后续更多的功能,大家可以自行探索。

总结概要

本文通过对pyvista这一相对“冷门”的python三维可视化工具的摸索,总结了安装与基本的使用示例,包括单模块、多模块、静态图、动态画板、gif和mp4视频的导出等。总体来说pyvista是一款渲染效果很棒的可视化软件,但是性能相对来说没有那么可观,对于规模不大的体系还是一个非常好用的工具,推荐使用。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/pyvista.html

作者ID:DechinPhy

更多原著文章请参考:https://www.cnblogs.com/dechinphy/

打赏专用链接:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

腾讯云专栏同步:https://cloud.tencent.com/developer/column/91958

posted @ 2021-07-23 17:52  DECHIN  阅读(7423)  评论(0编辑  收藏  举报