今天给大家分享一个神奇的 python 库,VisPy
VisPy 是一个高性能交互式 2D/3D 数据可视化库。VisPy 通过 OpenGL 库利用现代图形处理单元 (GPU) 的计算能力来显示非常大的数据集。
VisPy 的应用包括
-
拥有数百万个点的高质量交互式科学绘图。
-
实时数据的直接可视化。
-
3D 模型的快速交互式可视化。
-
OpenGL 可视化演示。
-
科学 GUI 具有快速、可扩展的可视化小部件。

VisPy 的优缺点
VisPy 优点
-
**性能高效:**利用GPU加速,可以渲染大型数据集,对实时可视化非常有效。
-
**易于使用:**提供了简洁的 API,对于不熟悉 OpenGL 的用户来说,可以快速上手创建可视化内容。
-
**灵活:**既有简单的快速绘图接口,也有基础接口,为用户提供了广泛的定制可能性。
-
**跨平台:**可以在Windows、macOS和Linux上运行。
VisPy 缺点
-
**OpenGL 依赖:**用户需要具备一定的 OpenGL 知识才能充分利用 VisPy 的所有功能。
-
**硬件要求:**对GPU有一定的要求,可能不支持旧的或性能较低的设备。
-
**文档和社区:**相比其他成熟的库,VisPy 的文档可能不那么全面,社区支持和资源也很少。
初体验
库的安装
可以直接使用 pip 来进行安装。
pip install --upgrade vispy
示例
下面我们来看一个官网给出的示例。
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
Plot clusters of data points and a graph of connections
"""
from vispy import app, scene, color
import numpy as np
# Initialize arrays for position, color, edges, and types for each point in
# the graph.
npts = 400
nedges = 900
ngroups = 7
np.random.seed(127396)
pos = np.empty((npts, 2), dtype='float32')
colors = np.empty((npts, 3), dtype='float32')
edges = np.empty((nedges, 2), dtype='uint32')
types = np.empty(npts, dtype=int)
# Assign random starting positions
pos[:] = np.random.normal(size=pos.shape, scale=4.)
# Assign each point to a group
grpsize = npts // ngroups
ptr = 0
typ = 0
while ptr < npts:
size = np.random.random() * grpsize + grpsize // 2
types[int(ptr):int(ptr+size)] = typ
typ += 1
ptr = ptr + size
# Randomly select connections, with higher connection probability between
# points in the same group
conn = []
connset = set()
while len(conn) < nedges:
i, j = np.random.randint(npts, size=2)
if i == j:
continue
p = 0.7 if types[i] == types[j] else 0.01
if np.random.random() < p:
if (i, j) in connset:
continue
connset.add((i, j))
connset.add((j, i))
conn.append([i, j])
edges[:] = conn
# Assign colors to each point based on its type
cmap = color.get_colormap('cubehelix')
typ_colors = np.array([cmap.map(x)[0, :3] for x in np.linspace(0.2, 0.8, typ)])
colors[:] = typ_colors[types]
# Add some RGB noise and clip
colors *= 1.1 ** np.random.normal(size=colors.shape)
colors = np.clip(colors, 0, 1)
# Display the data
canvas = scene.SceneCanvas(keys='interactive', show=True)
view = canvas.central_widget.add_view()
view.camera = 'panzoom'
view.camera.aspect = 1
lines = scene.Line(pos=pos, connect=edges, antialias=False, method='gl',
color=(1, 1, 1, 0.2), parent=view.scene)
markers = scene.Markers(pos=pos, face_color=colors, symbol='o',
parent=view.scene)
view.camera.set_range()
i = 1
def update(ev):
global pos, edges, lines, markers, view, force, dist, i
dx = np.empty((npts, npts, 2), dtype='float32')
dx[:] = pos[:, np.newaxis, :]
dx -= pos[np.newaxis, :, :]
dist = (dx**2).sum(axis=2)**0.5
dist[dist == 0] = 1.
ndx = dx / dist[..., np.newaxis]
force = np.zeros((npts, npts, 2), dtype='float32')
# all points push away from each other
force -= 0.1 * ndx / dist[..., np.newaxis]**2
# connected points pull toward each other
# pulsed force helps to settle faster:
s = 0.1
# s = 0.05 * 5 ** (np.sin(i/20.) / (i/100.))
# s = 0.05 + 1 * 0.99 ** i
mask = np.zeros((npts, npts, 1), dtype='float32')
mask[edges[:, 0], edges[:, 1]] = s
mask[edges[:, 1], edges[:, 0]] = s
force += dx * dist[..., np.newaxis] * mask
# points do not exert force on themselves
force[np.arange(npts), np.arange(npts)] = 0
force = force.sum(axis=0)
pos += np.clip(force, -3, 3) * 0.09
lines.set_data(pos=pos)
markers.set_data(pos=pos, face_color=colors)
i += 1
timer = app.Timer(interval=0, connect=update, start=True)
if __name__ == '__main__':
app.run()

题外话
当下这个大数据时代不掌握一门编程语言怎么跟的上时代呢?当下最火的编程语言Python前景一片光明!如果你也想跟上时代提升自己那么请看一下.

感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。

👉优快云大礼包🎁:全网最全《Python学习资料》免费赠送🆓!(安全链接,放心点击)
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。


二、Python必备开发工具
工具都帮大家整理好了,安装就可直接上手!
三、最新Python学习笔记
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

四、Python视频合集
观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

五、实战案例
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

六、面试宝典


简历模板

👉优快云大礼包🎁:全网最全《Python学习资料》免费赠送🆓!(安全链接,放心点击)
若有侵权,请联系删除
VisPy:高性能Python数据可视化库及其优缺点,
本文介绍了VisPy,一个利用GPU进行高性能2D/3D数据可视化的Python库,涵盖了其优点如高效、易用和灵活,以及对OpenGL依赖和硬件要求的注意点。还提供了一个使用示例和安装方法。
1472

被折叠的 条评论
为什么被折叠?



