Draw scatter_3d using plotly 4.10

gene_x 0 like s 244 view s

Tags: plot, python

Simplied but not working version.

import plotly.graph_objects as go
import pandas as pd
from sklearn.decomposition import PCA
import numpy as np

# Provided dataframe
df = pd.DataFrame({
    'PC1': [-13.999925, -12.504291, -12.443057, -13.065235, -17.316215],
    'PC2': [-1.498823, -3.342411, -6.067055, -8.205809, 3.293993],
    'PC3': [-3.335085, 15.207755, -14.725450, 15.078469, -6.917358],
    'condition': ['GFP d3', 'GFP d3', 'GFP d8', 'GFP d8', 'GFP+mCh d9/12'],
    'donor': ['DI', 'DII', 'DI', 'DII', 'DI']
})

# Create PCA plot with 3D scatter
fig = px.scatter_3d(df, x='PC1', y='PC2', z='PC3', color='condition', symbol='donor',
                    title='PCA with 3 dimensions')

# Custom legend for condition
conditions = df['condition'].unique()
colors = px.colors.qualitative.Plotly[:len(conditions)]

for i, cond in enumerate(conditions):
    fig.add_trace(go.Scatter3d(x=[None], y=[None], z=[None],
                               mode='markers',
                               marker=dict(size=6, color=colors[i]),
                               showlegend=True, name=cond))

# Custom legend for donor
donors = df['donor'].unique()
symbols = ['circle', 'diamond']

for i, donor in enumerate(donors):
    fig.add_trace(go.Scatter3d(x=[None], y=[None], z=[None],
                               mode='markers',
                               marker=dict(size=6, color='black', symbol=symbols[i]),
                               showlegend=True, name=donor))

# Annotations for the legend blocks
fig.update_layout(
    annotations=[
        dict(x=1.1, y=1.0, xref='paper', yref='paper', showarrow=False,
             text='Condition', font=dict(size=15)),
        dict(x=1.1, y=0.6, xref='paper', yref='paper', showarrow=False,
             text='Donor', font=dict(size=15))
    ]
)

fig.show()

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

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


© 2023 XGenes.com Impressum