✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Plotting masked surface

Hi all,

i am trying to overlay data over topography. My data set contains some extreme values. i would like to mask them with another color (grey for example) while using built-in continuous color scale. I have searched through forum but this solution is using custom color scale.custom color scale solution

Here is my code:

import pandas as pd
import numpy as np
import plotly.graph_objects as go 
from scipy.interpolate import griddata

# Topo Data
x_topo, y_topo, z_topo = np.genfromtxt(r'topo.txt', unpack=True)

z_topo = z_topo / 50000

#Mesh grid
xi_topo = np.linspace(min(x_topo), max(x_topo), num=100)
yi_topo = np.linspace(min(y_topo), max(y_topo), num=100)
xtopo_grid, ytopo_grid = np.meshgrid(xi_topo, yi_topo)

#Topo grid
topo = griddata((x_topo,y_topo),z_topo,(xtopo_grid,ytopo_grid),method='cubic')

#-------------------------------------------
#Model Data
x, y, z, d = np.genfromtxt(r'block_model.txt', unpack=True)

DATA_SET = np.vstack((x,y,z,d))
DATA_SET = DATA_SET.T
#-------------------------------------------

#This layer will be overlay surface topography
model_data = DATA_SET[DATA_SET[:,2] == -0.1 ]  

x_data = model_data[:,0]
y_data = model_data[:,1]
z_data = model_data[:,2]
d_data = model_data[:,3]

#Mesh grid
xi_data = np.linspace(min(x_data), max(x_data), num=100)
yi_data = np.linspace(min(y_data), max(y_data), num=100)
x_grid, y_grid = np.meshgrid(xi_data, yi_data)

#Data grid
model_layer = griddata((x_data,y_data),d_data,(x_grid,y_grid),method='cubic')

#------------------------------------------------------------------------------------

#Draw surface topography and overlay first layer
fig = go.Figure(go.Surface(x=xtopo_grid,
                           y=ytopo_grid,
                           z=topo,
                           surfacecolor=model_layer,  # -0.1
                           colorscale='jet_r',
                           cmin=-10.0,cmax=10.0
                           )
                )

# Bottom Layers

bottom_layers = [-0.2, -0.3, -0.4, -0.5] 

for i in bottom_layers:
    
    model_data = DATA_SET[DATA_SET[:,2] == i ]  

    d_data = model_data[:,3]

    # Grid Data (mesh grid remains the same for all layers)
    
    model_layer = griddata((x_data,y_data),d_data,(x_grid,y_grid),method='cubic')


    fig.add_trace(go.Surface(x=x_grid,
                             y=y_grid,
                             z=np.ones(shape=x_grid.shape)*i,
                             surfacecolor=model_layer,
                             colorscale='jet_r',
                             cmin=-10.0,cmax=10.0
                             )
                  )
                                                     
fig.update_layout(scene=dict(aspectratio=dict(x=1*2.4,
                                              y=1*1.2,
                                              z=1.8
                                              )

                             )
                  )                  
fig.show() 

My result:

Trying to plot something like below:
expected_result