scatter3D_plotly.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import pandas as pd
  2. import plotly.graph_objects as go
  3. # 示例数据
  4. data = {
  5. '机组名': ['机组A', '机组B', '机组C', '机组D'],
  6. '时间': ['2024-01-09 09:13:29', '2024-01-10 10:14:30', '2024-02-09 08:13:29', '2024-02-10 09:14:30'],
  7. '年月': ['2024-01', '2024-01', '2024-02', '2024-02'],
  8. '风速': [5.0, 6.0, 4.5, 5.5],
  9. '有功功率': [1000, 1200, 900, 1100]
  10. }
  11. df = pd.DataFrame(data)
  12. # 按风速升序排列数据
  13. df_sorted = df.sort_values(by='风速')
  14. # 获取唯一年月
  15. unique_months = df_sorted['年月'].unique()
  16. # 自定义颜色列表(确保颜色数量与唯一月份的数量相匹配)
  17. colors = ['red', 'blue', 'green', 'purple'] # 根据实际唯一月份数量调整颜色数量
  18. # 创建颜色映射
  19. color_map = dict(zip(unique_months, colors))
  20. # 使用go.Scatter3d创建3D散点图
  21. trace = go.Scatter3d(
  22. x=df_sorted['风速'],
  23. y=df_sorted['有功功率'],
  24. z=[color_map[month] for month in df_sorted['年月']],
  25. mode='markers',
  26. marker=dict(
  27. color=[color_map[month] for month in df_sorted['年月']],
  28. size=10,
  29. line=dict(color='rgba(255, 255, 255, 0.8)', width=0.5),
  30. opacity=0.8
  31. )
  32. )
  33. # 创建图形
  34. fig = go.Figure(data=[trace])
  35. # 更新图形的布局
  36. fig.update_layout(
  37. title='按风速升序排列的3D散点图:风速、有功功率与年月',
  38. margin=dict(l=0, r=0, b=0, t=0),
  39. scene=dict(
  40. xaxis=dict(title='风速'),
  41. yaxis=dict(title='有功功率'),
  42. zaxis=dict(
  43. title='年月',
  44. tickmode='array',
  45. tickvals=unique_months,
  46. ticktext=unique_months,
  47. categoryorder='category ascending'
  48. )
  49. )
  50. )
  51. # 显示图形
  52. fig.show()