gpt4 book ai didi

python - 控制seaborn热图中的单个线宽

转载 作者:行者123 更新时间:2023-12-05 00:52:21 26 4
gpt4 key购买 nike

是否可以加宽seaborn热图中特定列和行的线宽?

例如,这个热图可以

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, linewidths=1.0)

变成这样的东西:

enter image description here

最佳答案

这是可能的,但可能需要大量工作。可能的解决方案可能如下所示。它涉及绘制 6 个不同的热图并调整间距,使其看起来不错。然后还需要同步颜色缩放并手动设置颜色条。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()

data = np.random.rand(10, 12)

asp = data.shape[0]/float(data.shape[1])
figw = 8
figh = figw*asp

cmap = plt.cm.copper
norm = matplotlib.colors.Normalize(vmin= data.min(), vmax= data.max())

gridspec_kw = {"height_ratios":[9,1], "width_ratios" : [4,5,3]}
heatmapkws = dict(square=False, cbar=False, cmap = cmap, linewidths=1.0, vmin= data.min(), vmax= data.max() )
tickskw = dict(xticklabels=False, yticklabels=False)

left = 0.07; right=0.87
bottom = 0.1; top = 0.9
fig, axes = plt.subplots(ncols=3, nrows=2, figsize=(figw, figh), gridspec_kw=gridspec_kw)
plt.subplots_adjust(left=left, right=right,bottom=bottom, top=top, wspace=0.1, hspace=0.1*asp )
sns.heatmap(data[:9,0:4], ax=axes[0,0], xticklabels=False, yticklabels=True, **heatmapkws)
sns.heatmap(data[:9,4:9], ax=axes[0,1], xticklabels=False, yticklabels=False, **heatmapkws)
sns.heatmap(data[:9,9:12], ax=axes[0,2],xticklabels=False, yticklabels=False, **heatmapkws)

sns.heatmap(data[9:,:4], ax=axes[1,0], xticklabels=True, yticklabels=True, **heatmapkws)
sns.heatmap(data[9:,4:9], ax=axes[1,1], xticklabels=True, yticklabels=False, **heatmapkws)
sns.heatmap(data[9:,9:12], ax=axes[1,2], xticklabels=True, yticklabels=False,**heatmapkws)

axes[1,0].set_yticklabels([9])
axes[1,1].set_xticklabels([4,5,6,7,8])
axes[1,2].set_xticklabels([9,10,11])

cax = fig.add_axes([0.9,0.1,0.03,0.8])
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
fig.colorbar(sm, cax=cax)

plt.show()

enter image description here

关于python - 控制seaborn热图中的单个线宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com