logo

Seaborn Heatmap: una guía completa

Mapa de calor se define como una representación gráfica de datos que utiliza colores para visualizar el valor de la matriz. En esto, para representar valores más comunes o actividades más altas se usan colores más brillantes básicamente colores rojizos y para representar valores menos comunes o de actividad se prefieren colores más oscuros. El mapa de calor también se define por el nombre de la matriz de sombreado. Los mapas de calor en Seaborn se pueden trazar utilizando la función seaborn.heatmap().

seaborn.mapa de calor()

Sintaxis: seaborn. mapa de calor ( datos , * , vmin=Ninguno , vmax=Ninguno , cmap=Ninguno , centro=Ninguno , annot_kws=No , anchos de línea = 0 , color de línea = 'blanco' , cbar=Verdadero , **kwargs )

Parámetros importantes:



    datos: conjunto de datos 2D que se puede convertir en un ndarray. vmín, vmáx: Valores para anclar el mapa de colores; de lo contrario, se infieren a partir de los datos y otros argumentos de palabras clave. cmap: el mapeo de los valores de datos al espacio de color. centro: el valor en el que centrar el mapa de colores al trazar datos divergentes. annot: si es verdadero, escriba el valor de los datos en cada celda. fmt: código de formato de cadena que se utilizará al agregar anotaciones. linewidths: Ancho de las líneas que dividirán cada celda. linecolor: Color de las líneas que dividirán cada celda. cbar: si se debe dibujar una barra de colores.

Todos los parámetros excepto los datos son opcionales.

Devoluciones: Un objeto de tipo matplotlib.axes._subplots.AxesSubplot

Entendamos el mapa de calor con ejemplos.

Mapa de calor básico

Realización de un mapa de calor con los parámetros predeterminados. Crearemos datos 2-D de 10 × 10 usando el fecha() Función del módulo NumPy.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=> 1>,> >high>=> 100>,> >size>=> (>10>,>10>))> print>(>'The data to be plotted: '>)> print>(data)> > # plotting the heatmap> hm>=> sn.heatmap(data>=> data)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

The data to be plotted: [[46 30 55 86 42 94 31 56 21 7] [68 42 95 28 93 13 90 27 14 65] [73 84 92 66 16 15 57 36 46 84] [ 7 11 41 37 8 41 96 53 51 72] [52 64 1 80 33 30 91 80 28 88] [19 93 64 23 72 15 39 35 62 3] [51 45 51 17 83 37 81 31 62 10] [ 9 28 30 47 73 96 10 43 30 2] [74 28 34 26 2 70 82 53 97 96] [86 13 60 51 95 26 22 29 14 29]]>

Usaremos estos mismos datos en todos los ejemplos.

Anclando el mapa de colores

Si configuramos el mín. valor a 30 y el vmáx valor a 70, entonces solo se mostrarán las celdas con valores entre 30 y 70. A esto se le llama anclar el mapa de colores.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> vmin>=> 30> vmax>=> 70> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >vmin>=>vmin,> >vmax>=>vmax)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Elegir el mapa de colores

En esto, veremos el cmap parámetro. Matplotlib nos proporciona múltiples mapas de colores, puedes verlos todos aquí . En nuestro ejemplo, usaremos pestaña20 .

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cmap>=> 'tab20'> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cmap>=>cmap)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Centrar el mapa de colores

Centrar el cmap a 0 pasando el centro parámetro como 0.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cmap>=> 'tab20'> center>=> 0> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cmap>=>cmap,> >center>=>center)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Mostrando los valores de las celdas

Si queremos mostrar el valor de las celdas, entonces pasamos el parámetro ellos dicen como Verdadero. fmt se utiliza para seleccionar el tipo de datos del contenido de las celdas mostradas.

red de arquitectura

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> annot>=> True> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >annot>=>annot)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Personalizando la línea de separación

Podemos cambiar el grosor y el color de las líneas que separan las celdas usando el anchos de línea y color de linea parámetros respectivamente.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> linewidths>=> 2> linecolor>=> 'yellow'> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >linewidths>=>linewidths,> >linecolor>=>linecolor)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Ocultar la barra de colores

Podemos desactivar la barra de colores configurando el cbar parámetro en Falso.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cbar>=> False> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cbar>=>cbar)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción:

Quitar las etiquetas

Podemos deshabilitar la etiqueta x y la etiqueta y pasando False en el xticklabels y yticklabels parámetros respectivamente.

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> xticklabels>=> False> yticklabels>=> False> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >xticklabels>=>xticklabels,> >yticklabels>=>yticklabels)> > # displaying the plotted heatmap> plt.show()>

>

>

Producción: