#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time: 2024/8/2 9:49
# file: hotmap.py
# author: David
# company: shenyang JY
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import math

weather = pd.read_csv('./weather-1-process.csv')
power = pd.read_csv('./power.csv')
data = pd.merge(weather, power, on='C_TIME')
data['C_TIME'] = pd.to_datetime(data['C_TIME'])
data['时间1'] = data['C_TIME'].dt.strftime('%Y-%m')
data = data[['C_TIME', '时间1', 'C_AIRT', 'C_CELLT', 'C_DIFFUSER', 'C_DIRECTR', 'C_GLOBALR', 'C_REAL_VALUE']]
pearson = pd.DataFrame()
data.iloc[:, 2:] = data.iloc[:, 2:].astype(float)
indexs= []
new = {}
for name, group in data.groupby('时间1'):
    print("name=", name, "长度：", len(group))
    indexs.append(name)
#     new = {}
    for key, value in group.iloc[:, 2:].iteritems():
        cov = group[key].cov(group['C_REAL_VALUE'])
        var = group['C_REAL_VALUE'].var()
        vari = group[key].var()
        if var * vari != 0 :
            res_pearson = cov/math.sqrt(var*vari)
        else:
            res_pearson = 0
#         new[key] = res_pearson
        new.setdefault(key, []).append(res_pearson)
pearson = pd.DataFrame(new, index=indexs)
#     pearson = pearson.append(new, ignore_index=True)
# plt.style.use('ggplot')
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
fig,ax = plt.subplots(figsize=(50,6))
sns.set(font_scale=1.25)
# cmap = sns.cm.hot_r
hm = sns.heatmap(pearson, cbar=True, square=True, fmt='.2f', annot=True, annot_kws={'size': 10}, cmap="hot_r")
plt.show()