Draw scatter_3d using plotly 4.10

gene_x 0 like s 527 view s

Tags: plot, python

Simplied but not working version.

  1. import plotly.graph_objects as go
  2. import pandas as pd
  3. from sklearn.decomposition import PCA
  4. import numpy as np
  5. # Provided dataframe
  6. df = pd.DataFrame({
  7. 'PC1': [-13.999925, -12.504291, -12.443057, -13.065235, -17.316215],
  8. 'PC2': [-1.498823, -3.342411, -6.067055, -8.205809, 3.293993],
  9. 'PC3': [-3.335085, 15.207755, -14.725450, 15.078469, -6.917358],
  10. 'condition': ['GFP d3', 'GFP d3', 'GFP d8', 'GFP d8', 'GFP+mCh d9/12'],
  11. 'donor': ['DI', 'DII', 'DI', 'DII', 'DI']
  12. })
  13. # Create PCA plot with 3D scatter
  14. fig = px.scatter_3d(df, x='PC1', y='PC2', z='PC3', color='condition', symbol='donor',
  15. title='PCA with 3 dimensions')
  16. # Custom legend for condition
  17. conditions = df['condition'].unique()
  18. colors = px.colors.qualitative.Plotly[:len(conditions)]
  19. for i, cond in enumerate(conditions):
  20. fig.add_trace(go.Scatter3d(x=[None], y=[None], z=[None],
  21. mode='markers',
  22. marker=dict(size=6, color=colors[i]),
  23. showlegend=True, name=cond))
  24. # Custom legend for donor
  25. donors = df['donor'].unique()
  26. symbols = ['circle', 'diamond']
  27. for i, donor in enumerate(donors):
  28. fig.add_trace(go.Scatter3d(x=[None], y=[None], z=[None],
  29. mode='markers',
  30. marker=dict(size=6, color='black', symbol=symbols[i]),
  31. showlegend=True, name=donor))
  32. # Annotations for the legend blocks
  33. fig.update_layout(
  34. annotations=[
  35. dict(x=1.1, y=1.0, xref='paper', yref='paper', showarrow=False,
  36. text='Condition', font=dict(size=15)),
  37. dict(x=1.1, y=0.6, xref='paper', yref='paper', showarrow=False,
  38. text='Donor', font=dict(size=15))
  39. ]
  40. )
  41. fig.show()

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

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


© 2023 XGenes.com Impressum