import pandas as pd import plotly.graph_objects as go # 示例数据 data = { '机组名': ['机组A', '机组B', '机组C', '机组D'], '时间': ['2024-01-09 09:13:29', '2024-01-10 10:14:30', '2024-02-09 08:13:29', '2024-02-10 09:14:30'], '年月': ['2024-01', '2024-01', '2024-02', '2024-02'], '风速': [5.0, 6.0, 4.5, 5.5], '有功功率': [1000, 1200, 900, 1100] } df = pd.DataFrame(data) # 按风速升序排列数据 df_sorted = df.sort_values(by='风速') # 获取唯一年月 unique_months = df_sorted['年月'].unique() # 自定义颜色列表(确保颜色数量与唯一月份的数量相匹配) colors = ['red', 'blue', 'green', 'purple'] # 根据实际唯一月份数量调整颜色数量 # 创建颜色映射 color_map = dict(zip(unique_months, colors)) # 使用go.Scatter3d创建3D散点图 trace = go.Scatter3d( x=df_sorted['风速'], y=df_sorted['有功功率'], z=[color_map[month] for month in df_sorted['年月']], mode='markers', marker=dict( color=[color_map[month] for month in df_sorted['年月']], size=10, line=dict(color='rgba(255, 255, 255, 0.8)', width=0.5), opacity=0.8 ) ) # 创建图形 fig = go.Figure(data=[trace]) # 更新图形的布局 fig.update_layout( title='按风速升序排列的3D散点图:风速、有功功率与年月', margin=dict(l=0, r=0, b=0, t=0), scene=dict( xaxis=dict(title='风速'), yaxis=dict(title='有功功率'), zaxis=dict( title='年月', tickmode='array', tickvals=unique_months, ticktext=unique_months, categoryorder='category ascending' ) ) ) # 显示图形 fig.show()