Add two legends for two dimensions in scatter_3d() in plotly 4.10

gene_x 0 like s 338 view s

Tags: plot, python

add two legends for two dimensions in plotly 4.10 for the functio scatter_3d(), i.e. one legend for the color, one for the symbol

In Plotly 4.10, you can add two legends for two dimensions in scatter_3d() by creating a custom trace for each legend and adding them to the layout. Here's an example code

import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
z = [1, 2, 3, 4, 5]
color = [1, 1, 2, 2, 3]
symbol = [1, 2, 1, 2, 1]

# Create custom trace for color legend
color_legend_trace = go.Scatter3d(
    x=[],
    y=[],
    z=[],
    mode='markers',
    name='Color',
    marker=dict(
        size=5,
        opacity=0.7,
        colorscale='Viridis',
        color=[],
        showscale=True
    )
)

# Create custom trace for symbol legend
symbol_legend_trace = go.Scatter3d(
    x=[],
    y=[],
    z=[],
    mode='markers',
    name='Symbol',
    marker=dict(
        size=5,
        opacity=0.7,
        symbol=[],
        showscale=True
    )
)

# Populate custom traces with data
for i in range(len(x)):
    color_legend_trace['x'] += [x[i]]
    color_legend_trace['y'] += [y[i]]
    color_legend_trace['z'] += [z[i]]
    color_legend_trace['marker']['color'] += [color[i]]

    symbol_legend_trace['x'] += [x[i]]
    symbol_legend_trace['y'] += [y[i]]
    symbol_legend_trace['z'] += [z[i]]
    symbol_legend_trace['marker']['symbol'] += [symbol[i]]

# Create main scatter plot trace
scatter_trace = go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=5,
        opacity=0.7,
        colorscale='Viridis',
        color=color,
        symbol=symbol,
        showscale=False
    )
)

# Create layout with two legends
layout = go.Layout(
    scene=dict(
        aspectmode='data',
        legend=dict(
            itemsizing='constant'
        )
    ),
    legend=dict(
        orientation='h',
        yanchor='bottom',
        y=1.02,
        xanchor='right',
        x=1
    ),
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)

# Add custom traces and main scatter plot trace to figure
fig = go.Figure(data=[color_legend_trace, symbol_legend_trace, scatter_trace], layout=layout)

# Show figure
fig.show()

This code will create a 3D scatter plot with two legends: one for the color and one for the symbol. The color_legend_trace is a custom trace that shows the color legend, while the symbol_legend_trace is a custom trace that shows the symbol legend. The scatter_trace is the main scatter plot trace that contains the data points. In the layout, we set the legend property of the scene to None to remove the default legend, and add two custom legends using the legend property of the layout. Finally, we create a Figure object with the custom traces and the main scatter plot trace, and show it.


DEBUGGED!

import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
z = [1, 2, 3, 4, 5]
color = ['#AA0DFE', '#3283FE', '#85660D', '#782AB6', '#565656']
symbol = ['circle', 'circle-open', 'square', 'square-open', 'diamond']

# Create custom trace for color legend
color_legend_trace = go.Scatter3d(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    z=[1, 2, 3, 4, 5],
    mode='markers',
    name='Color',
    marker=dict(
        size=5,
        opacity=0.7,
        colorscale='Viridis',
        color=['#AA0DFE', '#3283FE', '#85660D', '#782AB6', '#565656'],
        showscale=True
    )
)

# Create custom trace for symbol legend
symbol_legend_trace = go.Scatter3d(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    z=[1, 2, 3, 4, 5],
    mode='markers',
    name='Symbol',
    marker=dict(
        size=5,
        opacity=0.7,
        symbol=['circle', 'circle-open', 'square', 'square-open', 'diamond'],
        showscale=True
    )
)

#Error due to the tuple type.
## Populate custom traces with data
#for i in range(len(x)):
#    color_legend_trace['x'] += [x[i]]
#    color_legend_trace['y'] += [y[i]]
#    color_legend_trace['z'] += [z[i]]
#    color_legend_trace['marker']['color'] += [color[i]]
#    
#    symbol_legend_trace['x'] += [x[i]]
#    symbol_legend_trace['y'] += [y[i]]
#    symbol_legend_trace['z'] += [z[i]]
#    symbol_legend_trace['marker']['symbol'] += [symbol[i]]

# Create main scatter plot trace
scatter_trace = go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=5,
        opacity=0.7,
        colorscale='Viridis',
        color=color,
        symbol=symbol,
        showscale=False
    )
)

#scene = go.Scene(
#    xaxis=dict(title='X Axis'),
#    yaxis=dict(title='Y Axis'),
#    zaxis=dict(title='Z Axis'),
#    # Remove the 'legend' property from the Scene object
#)

# Create layout with two legends
layout = go.Layout(
    #scene=dict(
    #    aspectmode='data'
    #),
    legend=dict(
        orientation='h',
        yanchor='bottom',
        y=1.02,
        xanchor='right',
        x=1
    ),
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)

# Add custom traces and main scatter plot trace to figure
fig = go.Figure(data=[color_legend_trace, symbol_legend_trace, scatter_trace], layout=layout)
#fig = go.Figure(data=[scatter], layout=go.Layout(
#    title='My 3D Scatter Plot',
#    legend=dict(
#        title='Legend Title',
#        font=dict(size=12)
#    )
#))

# Show figure
#fig.show()
fig.write_image("fig2.svg")

TODO: categorical legends!

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

看文章,发评论,不要沉默


© 2023 XGenes.com Impressum