VisPy, an amazing python library

Today I will share with you a magical python library, VisPy

VisPy is a high-performance interactive 2D/3D data visualization library. VisPy leverages the computing power of modern graphics processing units (GPUs) through the OpenGL library to display very large data sets.

Applications of VisPy include

  • High-quality interactive scientific plots with millions of points.

  • Direct visualization of real-time data.

  • Fast interactive visualization of 3D models.

  • OpenGL visualization demonstration.

  • Scientific GUI with fast, scalable visualization widgets.

Advantages and disadvantages of VisPy

VisPy Advantages

  1. **Performance Efficient: **Using GPU acceleration, large data sets can be rendered, which is very effective for real-time visualization.

  2. **Easy to use:** Provides a simple API, so users who are not familiar with OpenGL can quickly get started creating visual content.

  3. **Flexible:** It has both a simple quick drawing interface and a basic interface, providing users with a wide range of customization possibilities.

  4. **Cross-platform:** Runs on Windows, macOS and Linux.

VisPy Disadvantages

  1. **OpenGL Dependencies:** Users need to have some knowledge of OpenGL to take full advantage of all features of VisPy.

  2. **Hardware Requirements:** Has certain GPU requirements and may not support older or lower performance devices.

  3. **Documentation and Community:** Compared to other mature libraries, VisPy’s documentation may not be as comprehensive, and community support and resources are sparse.

First experience

Library installation

You can install it directly using pip.

pip install --upgrade vispy

Example

Let’s take a look at an example given on the official website.

# -*- 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
type = 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()


———————————END——————- ——–

Digression

In the current era of big data, how can one keep up with the times without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.


CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Essential development tools for Python

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)

If there is any infringement, please contact us for deletion.