技术背景

三维可视化是一项在工业领域中非常重要的技术,而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

PyVista:一款Python的三维可视化软件的更多相关文章

  1. 共有11款Python 中文分词库开源软件

    件过滤: 排序: 收录时间 | 浏览数 Python 中文分词库 Yaha "哑哈"中文分词,更快或更准确,由你来定义.通过简单定制,让分词模块更适用于你的需求. "Ya ...

  2. Python科学计算三维可视化(整理完结)

    中国MOOC<Pyhton计算计算三维可视化>总结 课程url:here ,教师:黄天宇,嵩天 下文的图片和问题,答案都是从eclipse和上完课后总结的,转载请声明. Python数据三 ...

  3. Avizo - 高级三维可视化及分析软件

    今天从材料科学应用角度介绍Avizo的基本功能. Avizo是一款先进的三维可视化及分析应用,可用来探索从断层扫描.显微镜.核磁共振成像及更多其他技术获得的材料科学数据.从简单的可视化与测量到高级的图 ...

  4. Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?

    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢? 可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的.对于初学者来说,很容易被这官网上 ...

  5. 多表查询思路、navicat可视化软件、python操作MySQL、SQL注入问题以及其他补充知识

    昨日内容回顾 外键字段 # 就是用来建立表与表之间的关系的字段 表关系判断 # 一对一 # 一对多 # 多对多 """通过换位思考判断""" ...

  6. 如何使用Python快速制作可视化报表----pyecharts

    如何使用Python快速制作可视化报表   数据可视化能力已经越来越成为各岗位的基础技能.领英的数据报告显示,数据可视化技能在2017年中国最热门技能中排名第一. 就数据分析而言,可视化探索几乎是你正 ...

  7. 基于 HTML5 WebGL 的计量站三维可视化监控系统 Web 组态工控应用

    得益于 HTML5 WebGL 技术的成熟,从技术上对工控管理的可视化,数据可视化变得简单易行!完成对工控设备的管理效率,资源管理,风险管理等的大幅度提高,同时也对国家工业4.0计划作出有力响应! 如 ...

  8. 科学计算三维可视化---Mayavi入门(Mayavi介绍和安装)

    Mayavi介绍 是基于VTK开发的可视化软件(更加高效),Mayavi完全由python编写,方便使用,而且可以使用python编写扩展,嵌入到用户程序中 安装要求 VTK >pip3 ins ...

  9. 科学计算三维可视化---TVTK入门(安装与测试)

    推文:http://docs.huihoo.com/scipy/scipy-zh-cn/tvtk_intro.html 推文:http://code.enthought.com/pages/mayav ...

随机推荐

  1. 目标检测中的anchor-based 和anchor free

    目标检测中的anchor-based 和anchor free 1.  anchor-free 和 anchor-based 区别 深度学习目标检测通常都被建模成对一些候选区域进行分类和回归的问题.在 ...

  2. TensorFlow基础剖析

    TensorFlow基础剖析 一.概述 TensorFlow 是一个使用数据流图 (Dataflow Graph) 表达数值计算的开源软件库.它使 用节点表示抽象的数学计算,并使用 OP 表达计算的逻 ...

  3. 重新整理 mysql 基础篇————— 介绍mysql[一]

    前言 准备整理mysql的基础篇了,前面整理了sql语句序列的的<sql 语句系列(八百章)>,感觉很多用不上,就停下来了,后续还是会继续整理. mysql 基础篇主要是对一些基础进行整理 ...

  4. MySQL泛泛而谈(3W字)

    下面对于MySQL进行相关介绍,文档的内容较为基础,仅仅设计操作,少量原理,大佬请绕道哦. 废话少说,开冲! 一.MySQL架构介绍 1-MySQL简介 概述 MySQL是一个关系型数据库管理系统,由 ...

  5. servlet、过滤器、监听器、拦截器之间的关系和区别

    一.概念 1.什么是servlet servlet是一个接口.定义了一套处理网络请求的规范,所有实现servlet的类,都需要实现它那五个方法,其中最主要的是两个生命周期方法 init()和destr ...

  6. CArray CList CMap 插入与遍历效率对比

    前言:程序中经常用到不定量数组,选择上可以使用CArray,CList,CMap,而这三者插入及遍历的效率,未测试过,随着数据量越来越大,需要做程序上的优化,于是比较下三种类型的插入盒遍历的效率. 一 ...

  7. Centos acme.sh 申请 LetsEncrypt 通配证书

    1. 安装 acme.sh 注意:如果需要使用 Standalone Mode请先安装socat# yum intall socat It is recommended to install soca ...

  8. ArcGIS连接Postgres 数据库

    ArcGIS连接Postgres 数据库 此前在使用ArcGIS的过程中,一般使用文件方式对数据进行管理,后面也有使用 GeoDatabase 数据库对数据进行管理,但是这种管理方式也存在一些弊端,特 ...

  9. zbxtable的使用

    实验环境: zabbix server 172.16.1.121 mysql 172.16.1.121 访问端 172.16.1.122 54.1 zbxtable 1 说明 ZbxTable使用Go ...

  10. POJ 1269 Intersecting Lines 判断两直线关系

    用的是初中学的方法 #include <iostream> #include <cstdio> #include <cstring> #include <al ...