Creating 3D Surface Plots with HoloViews

  • Share this:

Code introduction


This function creates a 3D surface plot using the HoloViews library. It accepts three arguments: x and y values for the grid, and a function f that calculates the z value for each grid point.


Technology Stack : HoloViews, NumPy

Code Type : Function

Code Difficulty : Intermediate


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

def plot_surface(x, y, f):
    """
    This function generates a 3D surface plot using HoloViews.
    """
    # Create a function for the surface plot
    f = np.vectorize(f)
    
    # Generate the grid for the plot
    x, y = np.meshgrid(x, y)
    
    # Create the surface plot
    surface = hv.Surface((x, y, f(x, y))).relabel("3D Surface Plot")
    
    # Set the plot options
    plot = surface.options(title="3D Surface Plot", width=600, height=400)
    
    return plot

# Example usage
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
z = np.sin(np.sqrt(x**2 + y**2))
plot = plot_surface(x, y, z)

# Display the plot
plot.show()