Теплова карта визначається як графічне представлення даних з використанням кольорів для візуалізації значення матриці. У цьому випадку для представлення більш поширених цінностей або вищої активності використовуються більш яскраві кольори, в основному червонуваті кольори, а для представлення менш поширених цінностей або цінностей активності перевага віддається темнішим кольорам. Теплова карта також визначається назвою матриці затінення. Теплові карти в Seaborn можна побудувати за допомогою функції seaborn.heatmap().
seaborn.heatmap()
Синтаксис: seaborn.heatmap( даних , * , vmin=Жодного , vmax=Жодного , cmap=Жодного , center=Жодного , annot_kws=Ні , ширина ліній=0 , linecolor=’білий’ , cbar=Правда , **кварги )
Важливі параметри:
дані: 2D-набір даних, який можна примусово перетворити в ndarray. vmin , vmax: Значення для закріплення карти кольорів, інакше вони виводяться з даних та інших ключових аргументів. cmap: зіставлення значень даних із простором кольорів. центр: значення, за яким центрується карта кольорів під час побудови розбіжних даних. annot: Якщо True, запишіть значення даних у кожну клітинку. fmt: код форматування рядка для використання під час додавання анотацій. linewidths: ширина ліній, які розділятимуть кожну клітинку. колір лінії: колір ліній, які розділятимуть кожну комірку. cbar: чи малювати кольорову смугу.
Усі параметри, крім даних, є необов’язковими.
Повернення: Об’єкт типу matplotlib.axes._subplots.AxesSubplot
Розберемо теплову карту на прикладах.
Базова теплова карта
Створення теплової карти з параметрами за замовчуванням. Ми будемо створювати двовимірні дані 10×10 за допомогою дата() функція модуля 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()> |
>
>
Вихід:
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]]>

Ми будемо використовувати ті самі дані в усіх прикладах.
Закріплення карти кольорів
Якщо ми встановимо хв значення до 30 і vmax значення до 70, то відображатимуться лише клітинки зі значеннями від 30 до 70. Це називається прив’язкою кольорової карти.
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()> |
>
>
Вихід:

Вибір карти кольорів
У цьому ми розглянемо cmap параметр. Matplotlib надає нам кілька кольорових карт, ви можете переглянути їх усі тут . У нашому прикладі ми будемо використовувати вкладка20 .
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()> |
>
>
Вихід:

Центрування карти кольорів
Центрування cmap до 0 шляхом передачі центр параметр як 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()> |
>
>
Вихід:

Відображення значень клітинок
Якщо ми хочемо відобразити значення комірок, ми передаємо параметр вони кажуть як Правда. fmt використовується для вибору типу даних для вмісту комірок, що відображаються.
bash розділив рядок роздільником
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()> |
>
>
Вихід:

Налаштування розділової лінії
Ми можемо змінити товщину та колір ліній, що розділяють клітинки, за допомогою ширини ліній і колір лінії параметри відповідно.
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()> |
>
>
Вихід:

Приховування кольорової панелі
Ми можемо вимкнути кольорову панель, встановивши cbar параметр на False.
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()> |
>
>
Вихід:

Видалення етикеток
Ми можемо вимкнути мітки x і y, передавши False у xticklabels і yticlabels параметри відповідно.
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()> |
>
>
Вихід:
