1

I would aim to colour a surface in a 3D plot, created using plotly.graph_objects.

I worked on an approach incorporating Mesh3d, the code runs, but the wanted surface is still not coloured. What is the mistake I made, or should another approach be used?

The final outcome is desired to look similar to this 3D plot: enter image description here

And this is where I am at now (I don't aim to apply color scaling to the surface, it is good enough to be filled with a consistent colour): enter image description here

So, the aim would be to colour the surface where the vertical lines are connecting the two curves (2D and the same in 3D).

Here is my code:

import plotly.graph_objects as go
import numpy as np

def plot_3d_course_profile(x, y, z, color_attribute, colorbar_label):
    fig = go.Figure()

    # Bottom curve at altitude = 350
    fig.add_trace(go.Scatter3d(
        x=x,
        y=y,
        z=[350] * len(x),
        mode='lines',
        line=dict(
            color='black',
            width=5,
        )
    ))

    # Profile curve
    fig.add_trace(go.Scatter3d(
        x=x,
        y=y,
        z=z,
        mode='lines',
        line=dict(
            color=color_attribute,
            width=13,
            colorscale='Jet',
            colorbar=dict(
                title=dict(
                    text=colorbar_label,
                    font=dict(size=14, color='black')
                ),
                thickness=20,
                len=0.6,
                tickfont=dict(size=12, color='black'),
                tickmode='linear',
                tickformat='.2f',
                outlinewidth=1,
                outlinecolor='black'
            )
        )
    ))

    # Vertical lines
    for i in range(0, len(x), 7):
        xi, yi, zi = x[i], y[i], z[i]
        fig.add_trace(go.Scatter3d(
            x=[xi, xi],
            y=[yi, yi],
            z=[350, zi],
            mode='lines',
            line=dict(color='dimgray', width=4),
            opacity=0.6,
            showlegend=False
        ))

        # surface - not displaying
    fig.add_trace(go.Mesh3d(
        x=np.concatenate([x, x[::-1]]),  
        y=np.concatenate([y, y[::-1]]),
        z=np.concatenate([[350] * len(x), z[::-1]]),
        color='blue',
        opacity=0.5,
        showscale=False
    ))
        
    fig.update_layout(
        template='plotly_white',
        scene=dict(
            xaxis=dict(title='Meters north from start position', showgrid=True, gridcolor='lightblue', zeroline=False),
            yaxis=dict(title='Meters east from start position', showgrid=True, gridcolor='lightblue', zeroline=False),
            zaxis=dict(title='Altitude [m]', showgrid=True, gridcolor='lightblue', zeroline=False),
            aspectmode='manual',
            aspectratio=dict(x=1.25, y=1, z=0.7)
        ),
        title=f'3D Course Profile Colored by {colorbar_label}',
        margin=dict(l=0, r=0, b=0, t=50)
    )

    fig.show() 

0

Browse other questions tagged or ask your own question.