【问题标题】:Difficulty plotting sequence of seaborn heatmaps难以绘制 seaborn 热图的序列
【发布时间】:2018-01-12 19:06:27
【问题描述】:

每当我绘制一个单一的 seaborn 热图时,我都会得到一个漂亮的结果,如下所示:

但是,当我尝试绘制一系列 seaborn 热图时,如下所示,我得到了非常奇怪的输出:

for i in range(5):

    # create mesh grid:
    res = 0.1 ## set resolution
    X,Y = 10, 10
    xy = np.mgrid[0:int(X):res, 0:int(Y):res].reshape(2,-1).T
    values = np.sum(xy,1)
    sns.heatmap(values.reshape((int(X/res),int(Y/res))),cmap="YlGnBu")

    plt.savefig(folder+"_"+str(i)+"_.png")

序列中的第一张图片很完美,但第二张看起来像这样:

第三个看起来像这样:

...等等。由于我无法理解的原因,seaborn 似乎每次都以某种方式添加一个新的颜色条。

当我将 cbar 设置为 False 时,这个问题不存在,但我实际上希望每个单独的热图都有一个颜色条。

【问题讨论】:

    标签: python-3.x data-visualization seaborn


    【解决方案1】:

    您似乎更想创建 5 个单独的人物。为了创建图形,您可以使用plt.figure()

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    for i in range(5):
        plt.figure()
        # create mesh grid:
        res = 0.1 ## set resolution
        X,Y = 10, 10
        xy = np.mgrid[0:int(X):res, 0:int(Y):res].reshape(2,-1).T
        values = np.sum(xy,1)
        sns.heatmap(values.reshape((int(X/res),int(Y/res))),cmap="YlGnBu")
    
        plt.savefig("seaborn_"+str(i)+"_.png")
    
    plt.close("All")
    

    您也可以使用plt.clf() 清除数字。

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    for i in range(5):
        plt.clf()
        # create mesh grid:
        res = 0.1 ## set resolution
        X,Y = 10, 10
        xy = np.mgrid[0:int(X):res, 0:int(Y):res].reshape(2,-1).T
        values = np.sum(xy,1)
        sns.heatmap(values.reshape((int(X/res),int(Y/res))),cmap="YlGnBu")
    
        plt.savefig("seaborn_"+str(i)+"_.png")
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 2018-11-23
      • 2018-11-29
      • 1970-01-01
      • 2022-01-21
      • 2015-06-17
      • 2019-10-21
      • 2017-07-12
      • 1970-01-01
      相关资源
      最近更新 更多