2016-04-27 53 views
6

有沒有辦法設置彩條尺度來登錄seaborn熱圖圖表?
我使用從大熊貓樞軸表輸出爲輸入到呼叫具有對數尺度彩條的Seaborn熱圖

sns.heatmap(df_pivot_mirror,annot=False,xticklabels=256,yticklabels=128,cmap=plt.cm.YlOrRd_r) 

謝謝。

回答

8

是的,但seaborn具有硬編碼的線性刻度定位的彩條,所以結果可能不會很你想要什麼:

# http://matplotlib.org/examples/pylab_examples/pcolor_log.html 
# modified to use seaborn 

import matplotlib.pyplot as plt 
from matplotlib.colors import LogNorm 
import numpy as np 
from matplotlib.mlab import bivariate_normal 
import seaborn as sns; sns.set() 


N = 20 
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] 

# A low hump with a spike coming out of the top right. 
# Needs to have z/colour axis on a log scale so we see both hump and spike. 
# linear scale only shows the spike. 
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 

fig, axs = plt.subplots(ncols=2) 

sns.heatmap(Z1, ax = axs[0]) 
sns.heatmap(Z1, ax = axs[1], 
      #cbar_kws={'ticks':[2,3]}, #Can't specify because seaborn does 
      norm=LogNorm(vmin=Z1.min(), vmax=Z1.max())) 


axs[0].set_title('Linear norm colorbar, seaborn') 
axs[1].set_title('Log norm colorbar, seaborn') 
plt.show() 

請參閱本開始爲一個pylab版本pylab例子自動獲取colorbar刻度標籤(儘管否則不會很漂亮)。

spiky data with linear and log colorbar

您可以編輯seaborn代碼,使其工作:如果你改變/seaborn/matrix.py的plot()功能(版本0.7.0):

# Possibly add a colorbar 
    if self.cbar: 
     ticker = mpl.ticker.MaxNLocator(6) 
     if 'norm' in kws.keys(): 
      if type(kws['norm']) is mpl.colors.LogNorm: 
       ticker = mpl.ticker.LogLocator(numticks=8) 

你:

enter image description here

我會建議在seaborn github上,但如果你想它前面,它就在那裏。

+1

seaborn問題https://github.com/mwaskom/seaborn/issues/459也使它可以解決(不同的方式) – cphlewis

+0

唯一的問題是如果你的一些價值觀是否定的,那麼這是一個問題。雙曲反sin(arcsinh)對於負值和0值的效果更好。 – wordsforthewise

+0

matplotlib沒有內置的arcsinh,但是可以使用SymLogNorm作爲負值。 不知道它是否處理0's。 https://matplotlib.org/devdocs/tutorials/colors/colormapnorms.html#sphx-glr-tutorials-colors-colormapnorms-py – wordsforthewise

1

迴應cphlewis(我沒有足夠的聲望),我用cbar_kws解決了這個問題;正如我在這裏看到的:seaborn clustermap: set colorbar ticks。例如cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}

s=np.random.rand(20,20) 
sns.heatmap(s, norm=LogNorm(s.min(),s.max()), 
      cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}, 
      vmin = 0.001, vmax=10000) 
plt.show() 

祝您有愉快的一天。