4

I would like to plot 3 stacked surfaces sharing the same colorbar and colorscale. I would like the colorbar to have 11 ticks in [0.0, 0.1, ..., 0.9, 1.0].

For some reason I can't seem to get plotly to use the same colorbar and scale for all the three graphs. Right now they all have their own colorscale. I can have it print the colorbar only once, but they will still have different scales.

Here is my code:

def plot_3d(plot_dict, title):
data = []
for i in range(3):
    dataset = plot_dict[kernels[i]]
    data.append(
        go.Surface(
            x=C_2d_range,
            y=gamma_2d_range,
            z=dataset, 
            #showscale=True if i == 0 else False, 
            colorbar=dict(
                nticks=11, 
                tickmode='array',
                tickvals=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
            ),
            colorscale='Viridis'
        )
    )

layout = go.Layout(
    title=title,
    scene = dict(
        xaxis = dict(
            title='C parameter'
        ),

        yaxis = dict(
            title='Gamma parameter'
        ),

        zaxis = dict(
            title='F1 Score'
        )
    )
)

fig = go.Figure(data=data, layout=layout)

ply.iplot(fig, filename=title)  

But it produces this where each plot is clearly using its own scale. Just to be clear in this case I would like to surface on the bottom to be dark and the one on the top to be yellow.

Thanks for any help.

ClonedOne
  • 569
  • 4
  • 20

1 Answers1

4

Fairly late to this question, but for future readers:

The trick to achieve this you need to set the same cmin and cmax of all surface objects:

import numpy as np

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import plot

z1 = np.random.uniform(0, 1, (10, 10))
z2 = z1 + 1
z3 = z1 - 1
min_value = np.r_[z1,z2,z3].min()
max_value = np.r_[z1,z2,z3].max()

surf1 = go.Surface(z=z1, cmin=min_value, cmax=max_value)
surf2 = go.Surface(z=z2, showscale=False, cmin=min_value, cmax=max_value)
surf3 = go.Surface(z=z3, showscale=False, cmin=min_value, cmax=max_value)

data = [surf1, surf2, surf3]

plot(data,filename='multiple-surfaces')

Result: Sample plot

YuppieNetworking
  • 8,672
  • 7
  • 44
  • 65
  • Unfortunately I don't have the code/data anymore so I can't try the solution, but it seems very nice in your example so I'll just accept it :) – ClonedOne Nov 09 '18 at 23:59