Exploring VisPy: A High-Performance Visualization Library in Python

Introduction

In the world of data science and scientific computing, visualization plays a crucial role in understanding and analyzing complex datasets. VisPy is a Python library designed specifically for high-performance interactive 2D and 3D data visualization. By leveraging OpenGL and GPU acceleration, VisPy enables users to render large datasets efficiently while maintaining interactivity. This makes it an excellent tool for scientific visualization, real-time data monitoring, and graphical applications requiring high-speed rendering. In this blog, we will explore what makes VisPy unique, how to install and use it, and why it stands out as a top-tier visualization library in Python. We will also look at its performance compared to other popular libraries, discuss real-world applications, and provide step-by-step guides for creating interactive plots.

Installation & Setup

To install VisPy, use:

pip install vispy

To verify installation, run:

import vispy
    print(vispy.sys_info())

The output of this should look something like the below, but will ultimately depend on your environment and your machine.

Platform: Linux-5.4.0-7642-generic-x86_64-with-debian-bullseye-sid
        Python:   3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 23:03:20)  [GCC 7.3.0]
        NumPy:    1.18.1
        Backend:  PyQt5
        pyqt4:    None
        pyqt5:    ('PyQt5', '5.12.3', '5.12.5')
        pyside:   None
        pyside2:  None
        pyglet:   None
        glfw:     None
        sdl2:     None
        wx:       None
        egl:      EGL 1.5 NVIDIA: OpenGL_ES OpenGL
        osmesa:   None
        _test:    None
        
        GL version:  '4.6.0 NVIDIA 455.28'
        MAX_TEXTURE_SIZE: 32768
        Extensions: 'GL_AMD_multi_draw_indirect ...'

For Running the programs smoothly in Python we need to install one of the recommended backends for VisPy Which enables the GUI applications and interactive graphics

PyQt6, One of the recommende backends can be installed by running the below command in the command prompt or terminal:

pip install PyQt6

To verify installation:

import PyQt6
print("PyQt6 successfully installed")
        

Why Choose VisPy?

Comparison between other existing libraries for plotting: VisPy vs Matplotlib vs Seaborn

Feature VisPy Matplotlib Seaborn
Rendering Engine OpenGL (GPU Accelerated) CPU-based CPU-based
Speed Extremely Fast for large data systems Slower for large data Slower for large data
Interactivity Real-time interaction Limited or negligible interactivity Limited or negligible interactivity
3D Support Yes (powerful 3D visualization) Basic (we can use mpl_toolkits.mplot3d for plotting basic plots) No 3D support
Ease of Use Moderate and requires OpenGL knowledge Beginner-friendly Beginner-friendly
Customization High (low-level control) High (but slower) Medium (contains pre-defined themes)
Best for For plotting Large-scale, real-time, interactive and 3D plots Static 2D plots, simple visualizations Statistical plotting includes correlations, distributions

Key Features

GPU Acceleration

Unlike traditional visualization libraries that rely on CPU-based rendering, VisPy takes advantage of GPU acceleration using OpenGL. This enables ultra-fast rendering, making it suitable for handling large datasets efficiently. With GPU-powered visualization, users can create interactive, real-time plots without experiencing lag, even when working with millions of data points.

Multiple Backends Support

VisPy integrates with multiple GUI frameworks, such as PyQt, PySide, GLFW, Tkinter, and Jupyter Notebooks. This flexibility allows developers to embed VisPy seamlessly into their applications, regardless of their preferred GUI toolkit.

Scientific Library Integration

One of the major strengths of VisPy is its compatibility with scientific computing libraries like NumPy, SciPy, and Pandas. Users can directly pass NumPy arrays into VisPy's visualization functions, making it an ideal choice for researchers and data analysts working with large numerical datasets.

2D and 3D Visualization

VisPy is highly versatile, supporting both 2D and 3D visualizations. Whether you're plotting simple line charts, scatter plots, heatmaps, or working with 3D surfaces, volumetric data, and interactive meshes, VisPy provides efficient rendering capabilities.

Custom Shader Support

For advanced users who require fine control over their visualizations, VisPy allows the use of custom shaders (https://vispy.org/overview.html)[ written in GLSL (OpenGL (https://vispy.org/overview.html) adding Language). This feature(https://github.com/vispy/vispy)] makes it possible to create unique visualization effects, such as real-time physics simulations, complex textures, and animated graphics.

Scene Graph API

VisPy includes a Scene Graph API, which simplifies the creation of complex, hierarchical visualizations. This is particularly useful when dealing with multiple interactive objects, such as 3D molecular models, network graphs, or game graphics.

Code Examples

Some Basic Codes Using VisPy

1. This programme plots a point in 3d space which allows rotation and zooming of point

import numpy as np
        from vispy import app, scene
        
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        view = canvas.central_widget.add_view()
        point = np.array([[0, 0]])
        marker = scene.visuals.Markers(parent=view.scene)
        marker.set_data(point, face_color='blue', size=10)
        view.camera = scene.PanZoomCamera()
        view.camera.set_range(x=(-1, 1), y=(-1, 1))
        app.run()
1

2. This is the programme to plot the graph of tan(x) in range -5 < x < 5

import numpy as np
        from vispy import app, scene
        from vispy.scene import visuals
        
        canvas = scene.SceneCanvas(keys='interactive', show=True, bgcolor='white', size=(800, 600))
        view = canvas.central_widget.add_view()
        view.camera = 'panzoom'
        view.camera.set_range(x=(-5, 5), y=(-2, 2))
        x = np.linspace(-5, 5, 100)
        y = np.tan(x)
        line = visuals.Line(pos=np.column_stack((x, y)), color='blue', width=2)
        view.add(line)
        if name == 'main':
            app.run()
2

3. This is the programme to make a 3D interactive scatter plot

import numpy as np
        from vispy import app, scene
        
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        view = canvas.central_widget.add_view()
        points = np.random.rand(100, 3) * 2 - 1 
        scatter = scene.visuals.Markers(parent=view.scene)
        scatter.set_data(points, face_color='red', size=8)
        view.camera = scene.cameras.TurntableCamera()
        
        app.run()
3

Some Advanced Codes Using VisPy which involves interactivity of the plot and allows rotation and zooming of the figure made. Here the camera control feature of VisPy are mainly used.

1. This is the programme to form interactive 3D cube

import numpy as np
        from vispy import app, scene
        
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        view = canvas.central_widget.add_view()
        vertices = np.array([[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
                             [-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]])
        edges = [(0, 1), (1, 2), (2, 3), (3, 0), 
                 (4, 5), (5, 6), (6, 7), (7, 4), 
                 (0, 4), (1, 5), (2, 6), (3, 7)] 
        edge_vertices = np.array([vertices[i] for edge in edges for i in edge])
        lines = scene.visuals.Line(pos=edge_vertices, color='white', connect='segments', parent=view.scene)
        view.camera = scene.cameras.TurntableCamera()
        view.camera.set_range(x=(-1, 1), y=(-1, 1), z=(-1, 1))
        if name == 'main':
            app.run()
1

2. This is the programme to form interactive 3D Pyramid

import numpy as np
        from vispy import app, scene
        
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        view = canvas.central_widget.add_view()
        vertices = np.array([[1, 1, 0], [-1, 1, 0], [-1, -1, 0], [1, -1, 0], [0, 0, 1]])  
        edges = [(0, 1), (1, 2), (2, 3), (3, 0),  (0, 4), (1, 4), (2, 4), (3, 4),  (0, 2), (1, 3)]  
        
        edge_vertices = np.array([vertices[i] for edge in edges for i in edge])
        lines = scene.visuals.Line(pos=edge_vertices, color='yellow', connect='segments', parent=view.scene)
        
        view.camera = scene.cameras.TurntableCamera()
        view.camera.set_range(x=(-1, 1), y=(-1, 1), z=(-1, 1))
        
        if name == 'main':
            app.run()
2

3. This is the programme to form interactive 3D Pyramid

import numpy as np
        from vispy import app, scene
        
        canvas = scene.SceneCanvas(keys='interactive', show=True, size=(600, 600))
        view = canvas.central_widget.add_view()
        x = np.linspace(-5, 5, 100)
        y = np.linspace(-5, 5, 100)
        X, Y = np.meshgrid(x, y)
        Z = np.sin(X + Y)
        surface = scene.visuals.SurfacePlot(x=X, y=Y, z=Z, color=(0.8, 0.3, 0.3, 1), parent=view.scene)
        view.camera = scene.cameras.TurntableCamera()
        view.camera.set_range(x=(-5, 5), y=(-5, 5), z=(-1, 1))
        canvas.show()
        app.run()
3

Below is the link to our jupyter notebook, which compises all the above code

Jupyter notebook

Use Cases

Conclusion

VisPy is an incredibly powerful library for anyone needing high-performance visualization in Python. Its combination of GPU acceleration, ease of integration with scientific tools, and support for interactive visualizations makes it an essential tool for data scientists, researchers, and developers alike. Whether you’re working with large-scale scientific data, real-time applications, or advanced graphics, VisPy offers a robust, efficient, and interactive solution. With GPU acceleration, it outperforms traditional libraries like Matplotlib in handling large-scale datasets. Whether you're working with 2D plots, 3D graphs, or real-time interactive visualizations, VisPy offers the flexibility and performance required for modern scientific computing.

References & Further Reading