How to 4D Surface plot with plotly backend?


I am trying to generate a Surface plot with color as 4th dimension. In plotly there is an attribute called surfacecolor which manaeges this. But I could not figure out how to do this in holoviews backend plotly. I need something like the following:

import plotly.graph_objects as go
import numpy as np
# Data to be plotted
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T
z = np.cos(x ** 2 + y ** 2)
c = np.linspace(5,10,900).reshape(30,30)
# plotting the figure
fig = go.Figure(data=[go.Surface(x=x, y=y, z=z, surfacecolor=c)])

Anyone who knows more about this feature? In case it is not possible, this information would be helpful.

I haven’t found a way without using a hook:

def hook_surface_color(plot, elem):
    del plot.handles["components"]["traces"][0]["colorscale"]
    plot.handles["components"]["traces"][0]["surfacecolor"] = c.tolist()
    plot.handles["components"]["traces"][0]["cmin"] = c.min()
    plot.handles["components"]["traces"][0]["cmax"] = c.max()
hv.Surface(z, kdims=["x","y"], vdims=["z"]).opts(cmap="plasma", height=300, width=600, hooks=[hook_surface_color])
1 Like

Thank you so much! Hooks are something new to me, I didnt know them yet. I’ll do it this way.

1 Like

Hi @beceti

Welcome to the community. Glad you found the solution. You can read about hooks here Customizing Plots — HoloViews v1.14.7.

1 Like