2016-04-27 17 views
6

C'è un modo per impostare la scala della barra dei colori per accedere a un grafico della mappa di calore generato dal mare?
Sto usando un'uscita tabella pivot da panda come input per la chiamataSeaborn Heatmap con barra dei colori a scala logaritmica

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

Grazie.

risposta

8

Sì, ma Seaborn ha codificato un localizzatore tick lineare per la barra colorata, quindi il risultato potrebbe non essere del tutto ciò che si vuole:

# 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() 

vedere l'esempio pylab questo è cominciato con per una versione pylab che ottiene automaticamente le etichette di spunta della barra di colore (sebbene non sia altrettanto bello).

spiky data with linear and log colorbar

È possibile modificare il codice di Seaborn per farlo funzionare: se si modifica la funzione di plot() in /seaborn/matrix.py (ver 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) 

si ottiene:

enter image description here

Ve lo proponiamo che sul GitHub Seaborn, ma se si vuole in precedenza, non v'è.

+1

numero Seaborn https://github.com/mwaskom/seaborn/issues/459 lo rende anche risolvibile (un modo diverso) – cphlewis

+0

L'unica cosa è che se alcuni dei tuoi valori sono negativi, allora questo è un problema. Il peccato inverso iperbolico (arcsinh) funziona meglio per valori negativi e 0. – wordsforthewise

+0

Non esiste un arcsinh incorporato in matplotlib, ma è possibile utilizzare SymLogNorm per i valori negativi. Non sono sicuro se gestisce gli 0. https://matplotlib.org/devdocs/tutorials/colors/colormapnorms.html#sphx-glr-tutorials-colors-colormapnorms-py – wordsforthewise

1

Rispondendo ai cphlewis (non ho abbastanza reputazione), ho risolto questo problema utilizzando cbar_kws; come ho visto qui: seaborn clustermap: set colorbar ticks.

Ad esempio 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() 

Buona giornata.