Generating a 3D Surface Plot with HoloViews

  • Share this:

Code introduction


This function generates a 3D surface plot using the HoloViews library. It first generates grid points for X and Y, then calculates the corresponding Z values, and finally creates a surface plot using the HoloViews Surface object.


Technology Stack : HoloViews, NumPy

Code Type : 3D Surface Plot

Code Difficulty : Intermediate


                
                    
import numpy as np
import holoViews as hv
from holoviews import opts

def plot_surface(arg1, arg2):
    # Generate a random 3D surface
    x = np.linspace(-5, 5, arg1)
    y = np.linspace(-5, 5, arg2)
    X, Y = np.meshgrid(x, y)
    Z = np.sin(np.sqrt(X**2 + Y**2))
    
    # Create a Holoviews surface plot
    surface = hv.Surface((X, Y, Z)).relabel('3D Surface')
    
    # Set the plot options
    plot = surfaceopts.Surface(
        colorbar=True, cmap='viridis', contours=True, contours_opts=dict levels=10
    )
    
    # Return the HoloViews plot object
    return plot