使用大熊猫创建多个热图图像,Seaborn?

时间:2019-10-28 05:53:26

标签: python pandas seaborn

当前,我正在尝试为我的数据输出图像输出。

我的原始来源是这样的:

total_dict = {"A" : {"A1" : [1, 2, 3, 4, 5], "A2" : [2, 3, 4, 5, 6]}, "B" : {"B1" : [11, 22, 13, 34, 5], "B2" : [12, 31, 42, 52, 16]},"C" : {"C1" : [12, 22, 33, 4, 5], "C2" : [42, 33, 42, 15, 6]}, "D" : {"D1" : [1, 23, 35, 4, 5], "D2" : [21, 23, 34, 5, 6]}}

现在,我正在尝试为每个子库(A,B,C和D)创建4个热图。 我的程序是:

import pandas as pd
import seaborn as sns
for sub in total_dict:
    df = pd.DataFrame(total_dict[sub])
    image = sns.heatmap(df, cmap="YlGnBu", linewidths = 0.1, vmax = 100)
    print (image)

但是,它不能像我预期的那样打印出4张单独的图像。最终结果是: heatmap_result

您能否建议我使用任何一种方法来获取预期的4个单独的输出,并将其保存在4个单独的文件中。 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以定义一个新图形,绘制热图并保存该图形。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

for index, sub in enumerate(total_dict):
  df = pd.DataFrame(total_dict[sub])
  # create a new figure
  fig = plt.figure()
  image = sns.heatmap(df, cmap="YlGnBu", linewidths = 0.1, vmax = 100)
  fig_name = 'test_%s.png' %(index)
  fig.savefig(fig_name)