【问题标题】:How to create a seaborn.heatmap() with frames around the tiles?如何在瓷砖周围创建一个带有框架的 seaborn.heatmap()?
【发布时间】:2016-04-11 23:06:53
【问题描述】:

我使用 seaborn.heatmap() 渲染了一张热图,效果很好。但是,出于某种目的,我需要情节周围的框架。

matplotlib.rcParams['axes.edgecolor'] = 'black'
matplotlib.rcParams['axes.linewidth'] = 1

两者都不起作用。

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:
    ax = sns.heatmap(x)
    for _, spine in ax.spines.items():
        spine.set_visible(True)
    

    【讨论】:

      【解决方案2】:

      sns.heatmap(dataset, linewidths=1, linecolor='black')

      【讨论】:

      • 采用这种方法,外线只有内线宽度的一半。
      • @DiddiZ sns.heatmap(dataset, linewidths=1, linecolor='black', clip_on=False) 会避免外线被剪裁。
      【解决方案3】:

      不知道是否有技术命令,但如果您想模仿这种行为,请尝试使用 axhlineaxvline

      import string
      import numpy as np
      import pandas as pd
      import seaborn as sns
      import matplotlib.pyplot as plt
      letters = string.ascii_letters
      
      rs = np.random.RandomState(33)
      d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                       columns=list(letters[:26]))
      
      # Compute the correlation matrix
      corr = d.corr()
      
      # Generate a mask for the upper triangle
      mask = np.zeros_like(corr, dtype=np.bool)
      mask[np.triu_indices_from(mask)] = True
      
      # Set up the matplotlib figure
      f, ax = plt.subplots(figsize=(11, 9))
      
      # Generate a custom diverging colormap
      cmap = sns.diverging_palette(220, 10, as_cmap=True)
      
      # Draw the heatmap with the mask and correct aspect ratio
      ax = sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
                  square=True, xticklabels=5, yticklabels=5,
                  linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
      
      ax.axhline(y=0, color='k',linewidth=10)
      ax.axhline(y=corr.shape[1], color='k',linewidth=10)
      ax.axvline(x=0, color='k',linewidth=10)
      ax.axvline(x=corr.shape[0], color='k',linewidth=10)
      plt.show()
      

      ,结果为:

      【讨论】:

      • 除了热图网格之外,我最终还使用了轴线,因为我希望外边界线与内线具有相同的宽度。
      猜你喜欢
      • 2012-05-26
      • 2018-02-18
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多