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.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,>=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.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))
sphere2 = pv.Sphere(radius=0.1,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,0))+pv.Sphere(radius=0.3,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

原文地址:https://www.cnblogs.com/dechinphy

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Python中的函数(二) 在上一篇文章中提到了Python中函数的定义和使用,在这篇文章里我们来讨论下关于函数的一些更深的话题。在学习C语言函数的时候,遇到的问题主要有形参实参的区别、参数的传递和改变、变量的作用域。同样在Python中,关于对函数的理解和使用也存在这些问题。下面来逐一讲解。一.函
Python中的字符串 可能大多数人在学习C语言的时候,最先接触的数据类型就是字符串,因为大多教程都是以&quot;Hello world&quot;这个程序作为入门程序,这个程序中要打印的&quot;Hello world&quot;就是字符串。如果你做过自然语言处理方面的研究,并且用Python
Python 面向对象编程(一) 虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程。下面就来了解一下如何在Python中进行对象编程。一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法。 类是对现实世界中一些事物的封装,
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定义和使用方法,这只体现了面向对象编程的三大特点之一:封装。下面就来了解一下另外两大特征:继承和多态。 在Python中,如果需要的话,可以让一个类去继承一个类,被继承的类称为父类或者超类、也可以称作基类,继承的类称为子类。并且Pytho
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非常熟悉,无论在哪门编程语言当中,函数(当然在某些语言里称作方法,意义是相同的)都扮演着至关重要的角色。今天就来了解一下Python中的函数用法。一.函数的定义 在某些编程语言当中,函数声明和函数定义是区分开的(在这些编程语言当中函数声明
在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方便和顺手,就是web.py。它由一名黑客所创建,但是不幸的是这位创建者于2013年自杀了。据说现在由
将Sublime Text 2搭建成一个好用的IDE 说起编辑器,可能大部分人要推荐的是Vim和Emacs,本人用过Vim,功能确实强大,但是不是很习惯,之前一直有朋友推荐SUblime Text 2这款编辑器,然后这段时间就试了一下,就深深地喜欢上这款编辑器了...
Python中的模块 有过C语言编程经验的朋友都知道在C语言中如果要引用sqrt这个函数,必须用语句&quot;#include&lt;math.h&gt;&quot;引入math.h这个头文件,否则是无法正常进行调用的。那么在Python中,如果要引用一些内置的函数,该怎么处理呢?在Python中
Python的基础语法 在对Python有了基础的认识之后,下面来了解一下Python的基础语法,看看它和C语言、java之间的基础语法差异。一.变量、表达式和语句 Python中的语句也称作命令,比如print &quot;hello python&quot;这就是一条语句。 表达式,顾名思义,是
Eclipse+PyDevʽjango+Mysql搭建Python web开发环境 Python的web框架有很多,目前主流的有Django、Tornado、Web.py等,最流行的要属Django了,也是被大家最看好的框架之一。下面就来讲讲如何搭建Django的开发环境。一.准备工作 需要下载的
在windows下安装配置Ulipad 今天推荐一款轻便的文本编辑器Ulipad,用来写一些小的Python脚本非常方便。 Ulipad下载地址: https://github.com/limodou/ulipad http://files.cnblogs.com/dolphin0520/u...
Python中的函数(三) 在前面两篇文章中已经探讨了函数的一些相关用法,下面一起来了解一下函数参数类型的问题。在C语言中,调用函数时必须依照函数定义时的参数个数以及类型来传递参数,否则将会发生错误,这个是严格进行规定的。然而在Python中函数参数定义和传递的方式相比而言就灵活多了。一.函数参数的
在Notepad++中搭配Python开发环境 Python在最近几年一度成为最流行的语言之一,不仅仅是因为它简洁明了,更在于它的功能之强大。它不仅能够完成一般脚本语言所能做的事情,还能很方便快捷地进行大规模的项目开发。在学习Python之前我们来看一下Python的历史由来,&quot;Pytho
Python中的条件选择和循环语句 同C语言、Java一样,Python中也存在条件选择和循环语句,其风格和C语言、java的很类似,但是在写法和用法上还是有一些区别。今天就让我们一起来了解一下。一.条件选择语句 Python中条件选择语句的关键字为:if 、elif 、else这三个。其基本形式如
关于raw_input( )和sys.stdin.readline( )的区别 之前一直认为用raw_input( )和sys.stdin.readline( )来获取输入的效果完全相同,但是最近在写程序时有类似这样一段代码:import sysline = sys.stdin.readline()
初识Python 跟学习所有的编程语言一样,首先得了解这门语言的编程风格和最基础的语法。下面就让我们一起来了解一下Python的编程风格。1.逻辑行与物理行 在Python中有逻辑行和物理行这个概念,物理行是指在编辑器中实际看到的一行,逻辑行是指一条Python语句。在Python中提倡一个物理行只
当我们的代码是有访问网络相关的操作时,比如http请求或者访问远程数据库,经常可能会发生一些错误,有些错误可能重新去发送请求就会成功,本文分析常见可能需要重试的场景,并最后给出python代码实现。
1.经典迭代器 2.将Sentence中的__iter__改成生成器函数 改成生成器后用法不变,但更加简洁。 3.惰性实现 当列表比较大,占内存较大时,我们可以采用惰性实现,每次只读取一个元素到内存。 或者使用更简洁的生成器表达式 4.yield from itertools模块含有大量生成器函数可
本文介绍简单介绍socket的常用函数,并以python-kafka中的源码socketpair为例,来讲解python socket的运用
python实践中经常出现编码相关的异常,大多网上找资料而没有理解原理,导致一次次重复错误。本文对常用Unicode、UTF-8、GB2312编码的原理进行介绍,接着介绍了python字符类型unicode和str以及常见编解码错误UnicodeEncodeError和UnicodeDEcodeEr