簡體   English   中英

Seaborn 熱圖不斷在數據集的循環中添加彩條

[英]Seaborn heatmap keeps adding color bars in loop over datasets

我正在嘗試在各種數據集上循環使用 plot 熱圖,但是對於每個新的熱圖,都會添加一個新的顏色條(map 看起來很好,並且沒有添加額外的地圖)。 我可以通過重置循環內的顏色條來使用解決方法,但我更願意知道發生了什么並有一個更清潔的解決方案。 提前感謝您的幫助!

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns; 

# read file
atlas = ['A','B','C','D']
output_path = '/Users/polo/Desktop/Heatmaps/'

for at in range(len(atlas)):
    data = pd.read_csv('/Users/polo/Desktop/correl_input_{}.csv'.format(atlas[at]))
    hmap = sns.heatmap(data,cmap='seismic',linewidths=.5,vmin=-0.1, vmax=0.1)
    hmap.set_ylim(0, 5)
    plt.savefig(output_path + 'Heatmap_{}.png'.format(atlas[at]), dpi=1200,bbox_inches='tight')
    #plt.savefig('{}_plot.png', format='png', dpi=1200,bbox_inches='tight')

TL; DR 您的plt.figure()解決方案是正確的解決方案, 如此處所建議。

建立在 matplotlib 之上,seaborn 使用圖形和軸的概念。 創建matplotlib.pyplot圖表的規范方法從使用f, ax = plt.subplots()實例化圖形和軸開始。 當您調用軸級別的 plot 時,例如 heatmap , heatmap 調用matplotlib.pyplot.gca() ,它獲取當前軸。 如果它不存在,seaborn 在后台實例化一個新的。

我猜在你的循環中,熱圖相互覆蓋,但 seaborn 正在動態調整圖形為每個顏色條留出空間。 plt.figure() (或f, ax = plt.subplots() )清除圖形是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM