python热力图实现
boy918.:
你好 你参考下下面的代码
import seaborn as sns
import matplotlib.pyplot as plt
# 生成热力图数据
data = sns.load_dataset("flights").pivot("month", "year", "passengers")
# 设置图形尺寸
plt.figure(figsize=(8, 6))
# 生成热力图
heatmap = sns.heatmap(data, annot=True, fmt="d", cmap="YlGnBu")
# 将列标签显示在方格中央
for t in heatmap.texts:
if t.get_text().isdigit():
t.set_text("{:.0f}".format(float(t.get_text())))
t.set_ha('center')
t.set_va('center')
else:
t.set_text("")
t.set_ha('center')
t.set_va('center')
# 显示图形
plt.show()
|