Search code examples
pythonplotlyvisualizationplotly-python

Adjusting the plotly colorbar for each subplot according to their min and max


I wanted to find out how to have three different colorbars for my plotly 3 subplots and be able to adjust each of the three colorbars according to their min and max. (attached snapshot).

  1. Does anyone know how to have each colorbar on the right side of each subplot?

  2. Also, for some reasons, the plot sizes are not perfect and they dont appear with the subtitles specified in the code!

  3. Lastly, I wonder if there is a way to synchronize the subplots together so that when we zoom in or out on each of the subplots, they all move together.

enter image description here This is a sample of my code:

    import pandas as pd
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots

    # load dataset
    Real_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
    Model_df = (df_1[np.random.default_rng(seed=42).permutation(df_1.columns.values)])
    Error_df = pd.DataFrame(np.random.randint(0,5,size=(87, 61)), columns=df_2.columns)



    # create figure
    #fig = go.Figure()

    # Add surface trace

    fig = make_subplots(rows=1, cols=3,
                        specs=[[{'is_3d': True}, {'is_3d': True}, {'is_3d': True}]],
                        subplot_titles=['True', 'Model', 'Error Percentage'],
                        )


    fig.add_trace(go.Surface(z=Real_df.values.tolist(), colorscale="jet"), 1, 1)
    fig.add_trace(go.Surface(z=Model_df.values.tolist(), colorscale="jet"), 1, 2)
    fig.add_trace(go.Surface(z=Error_df.values.tolist(), colorscale="jet"), 1, 3)

    # Update plot sizing
    fig.update_layout(
        width=800,
        height=900,
        autosize=False,
        margin=dict(t=0, b=0, l=0, r=0),
        template="plotly_white",
    )

    # Update 3D scene options
    fig.update_scenes(
        aspectratio=dict(x=1, y=1, z=0.7),
        aspectmode="manual"
    )

    fig.update_layout(1, 3, coloraxis={"cmin": 0, "cmax": 2})   


    fig.show()

Solution

  • To display a color bar for each subplot, the x-axis position must be set for each subplot. Also, for the subplot titles, the top margin is set to 0, which hides the text display area, so I set the top margin to 50. Finally, there does not seem to be a way to synchronize the zoom of the subplots at this time; the plotly community has mentioned synchronizing the camera viewpoint as an answer, but I am unsure if that is available in the current version.

    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    
    # load dataset
    Real_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
    Model_df = (Real_df[np.random.default_rng(seed=42).permutation(Real_df.columns.values)])
    Error_df = pd.DataFrame(np.random.randint(0,5,size=(87, 61)), columns=Real_df.columns)
    
    # Add surface trace
    fig = make_subplots(rows=1, cols=3,
                        specs=[[{'is_3d': True}, {'is_3d': True}, {'is_3d': True}]],
                        subplot_titles=['True', 'Model', 'Error Percentage'],
                        )
    
    fig.add_trace(go.Surface(z=Real_df.values.tolist(), colorscale="jet", colorbar_x=0.3), 1, 1)
    fig.add_trace(go.Surface(z=Model_df.values.tolist(), colorscale="jet", colorbar_x=0.65), 1, 2)
    fig.add_trace(go.Surface(z=Error_df.values.tolist(), colorscale="jet", colorbar_x=1.05, cmax=2, cmin=0), 1, 3)
    
    # Update plot sizing
    fig.update_layout(
        width=2000,
        height=900,
        autosize=False,
        margin=dict(t=50, b=0, l=0, r=0),
        template="plotly_white",
    )
    
    # Update 3D scene options
    fig.update_scenes(
        aspectratio=dict(x=1, y=1, z=0.7),
        aspectmode="manual"
    )
    
    fig.show()
    

    enter image description here