import pandas as pd
import numpy as np
import plotly.graph_objects as go

# 示例数据
data = {
    'power': np.random.uniform(0, 500, 1000),  # 假设功率范围是 0 到 500 kW
    'time': pd.date_range(start='2024-01-01', periods=240, freq='H'),  # 假设时间范围是 1000 小时
    'temperature': np.random.uniform(0, 100, 1000)  # 假设温度范围是 0 到 100 °C
}
df = pd.DataFrame(data)

# 功率分仓
df['power_bin'] = (df['power'] // 25) * 25

# 时间分仓（按天）
df['date'] = df['time'].dt.date

# 温度分仓
df['temperature_bin'] = (df['temperature'] // 5) * 5

# 计算每个分仓的出现次数
heatmap_data = df.groupby(['power_bin', 'date', 'temperature_bin']).size().reset_index(name='count')

# 创建3D热图
fig = go.Figure(data=[
    go.Scatter3d(
        x=heatmap_data['power_bin'],
        y=heatmap_data['date'],
        z=heatmap_data['temperature_bin'],
        mode='markers',
        marker=dict(
            size=5,
            color=heatmap_data['count'],
            colorscale='Viridis',
            colorbar=dict(title='Count'),
        )
    )
])

fig.update_layout(
    title='功率，时间和温度的3D热图',
    scene=dict(
        xaxis_title='功率 (kW)',
        yaxis_title='日期',
        zaxis_title='温度 (°C)'
    )
)

fig.show()
