在同一图中绘制循环的结果-热图

时间:2019-02-11 21:41:22

标签: python

我创建了一个具有随机列和值的数据框。现在,我试图通过“时间”窗口进行循环迭代(也许有比我更优雅的解决方案),我尝试在热图中绘制计算出的相关性,然后进行迭代,并在同一图中显示下一个结果。 像这样 https://datasoaring.blogspot.com/2018/07/gdp-correlation-matrix-top-10-economies.html

当前代码为每个相关性绘制一个新数字...

感谢您的想法和帮助!

  

创建数据框

import pandas as pd
import numpy as np


%matplotlib inline
import matplotlib.pyplot as plt
import time

import seaborn as sns
sns.set_style('white')


plt.style.use('dark_background')

index = pd.date_range('01/01/2010',periods=num_days, freq='D')
data_KW = pd.DataFrame(np.random.randint(0,250,size=(250, 10)), columns=list('ABCDEFGHIJ'), index=index)

data_KW.head()
  

interate and plot(wrong:))

# Calculate the lenght of the Dataframe
end = 10 #len(data_KW.index)

# is the variable for the rolling window
var_start = 0
var_end = 5

#Set up the matplotlib figure
f, ax = plt.subplots(figsize=(5, 5))


while var_end <= end:
    window = data_KW.iloc[var_start : var_end]

    # Compute the correlation matrix
    corr = window.corr()

    # Generate a mask for the upper triangle
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True


    # Generate a custom diverging colormap
    cmap = sns.diverging_palette(220, 10, as_cmap=True)

    # Draw the heatmap with the mask and correct aspect ratio
    sns.heatmap(corr, mask=mask, cmap=cmap, vmax=1, center=0,
                square=True, linewidths=.5, cbar_kws={"shrink": .5})

    #plt.pause(3)
    plt.show()
    time.sleep(2)

    #time.sleep(5)
    var_start = var_start + 1
    var_end = var_end + 1

    print(var_start)

0 个答案:

没有答案