2017-08-11 63 views
0

我試圖使用seaborn的熱圖可視化數據幀。 數據中的最小數量是-1.25,如下所示。但是,熱圖編碼範圍從-80(藍色)。我怎樣才能使顏色編碼從藍色-1.25開始,到紅色97?Seaborn顏色地圖顯示數據中不存在

我試過使用vmax,vmin和強大的關鍵字,但他們沒有幫助。

f, ax = plt.subplots(figsize=(8, 20)) 
       sns.heatmap(hill, annot=annot, linewidths=1, ax=ax, fmt='g', vmin=hill.values.min(), vmax=hill.values.max(), robust=True) 
       plt.yticks(rotation=0) 



hill.values.min() 
Out[30]: -1.25 
hill.values.max() 
Out[31]: 96.899999999999991 

enter image description here

+0

在你的heatmap中使用'hill.values.min()',而在隨後的計算中使用'hill.min()。min()'。爲什麼? – asongtoruin

+0

@ason​​gtoruin感謝您指出。我正在嘗試不同的方式來獲得最小值。情節是一樣的。我更新了這個帖子以保持一致。 – Lisa

+0

當你設置'vmin'和'vmax'時,你有什麼理由爲什麼'robust = True'?也許嘗試沒有這 – asongtoruin

回答

0

首先,你需要的功能是在0.8或更高版本seaborn默認。在這個版本的代碼

import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 

arr1 = np.linspace(-1.25,97,100).reshape(10,10) 

sns.heatmap(arr1, cmap="coolwarm") 

plt.show() 

產生

enter image description here

一個容易的選擇是因此更新您的seaborn。


在0.8以下的版本seaborn您可以通過指定使用center參數顏色映射中心獲得相同的行爲。

import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 

arr1 = np.linspace(-1.25,97,100).reshape(10,10) 

center = np.mean([arr1.min(), arr1.max()]) 
sns.heatmap(arr1, cmap="coolwarm", center=center, vmin=arr1.min(), vmax=arr1.max()) 

plt.show()