How to Read Heat Map in Python

Heatmap Basics with Seaborn

A guide for how to create heatmaps with Matplotlib and Seaborn

Thiago Carvalho

Correlation Matrix — Composition of a sample of Cereals

Hands-on

              import pandas every bit pd
import matplotlib.pyplot as plt
import seaborn equally sb
import numpy as np

Fatigued with sketchpad.app
              # read file
df = pd.read_csv('data/Foreign_Exchange_Rates.csv',
usecols=[1,7], names=['Date', 'CAD_USD'],
skiprows=1, index_col=0, parse_dates=[0])

Data frame.
              df['CAD_USD'] = pd.to_numeric(df.CAD_USD, errors='coerce')
df.dropna(inplace=True)
              # create a copy of the dataframe, and add columns for month and year
df_m = df.re-create()
df_m['calendar month'] = [i.calendar month for i in df_m.index]
df_m['year'] = [i.year for i in df_m.index]
# group past calendar month and year, get the boilerplate
df_m = df_m.groupby(['calendar month', 'twelvemonth']).mean()

The beginning 5 rows grouped.
              df_m = df_m.unstack(level=0)            

The reshaped data frame.

Colormaps

              fig, ax = plt.subplots(figsize=(11, nine))              sb.heatmap(df_m)              plt.bear witness()            

First heatmap.
              fig, ax = plt.subplots(figsize=(11, ix))              # plot heatmap
sb.heatmap(df_m, cmap="Blues", vmin= 0.nine, vmax=ane.65,
linewidth=0.three, cbar_kws={"compress": .8})
plt.show()

Second heatmap.

Customizing

              # effigy
fig, ax = plt.subplots(figsize=(11, 9))
# plot heatmap
sb.heatmap(df_m, cmap="Dejection", vmin= 0.9, vmax=ane.65, square=True,
linewidth=0.iii, cbar_kws={"compress": .viii})
# xticks
ax.xaxis.tick_top()
xticks_labels = ['Jan', 'February', 'Mar', 'April', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
plt.xticks(np.arange(12) + .5, labels=xticks_labels)
# axis labels
plt.xlabel('')
plt.ylabel('')
# championship
title = 'monthly Average exchange rate\nValue of ane USD in CAD\n'.upper()
plt.title(title, loc='left')
plt.show()

Final heatmap.

Correlation Matrix

              # read dataset
df = pd.read_csv('data/cereal.csv')
# go correlations
df_corr = df.corr()
# irrelevant fields
fields = ['rating', 'shelf', 'cups', 'weight']
# driblet rows
df_corr.drop(fields, inplace=True)
# drib cols
df_corr.drop(fields, axis=ane, inplace=True)

The cereals data frame.

Masks

              np.ones_like(df_corr, dtype=np.bool)            

Matrix of ones (boolean)
              mask = np.triu(np.ones_like(df_corr, dtype=np.bool))            

Mask

First Correlation Matrix.
              fig, ax = plt.subplots(figsize=(10, 8))              # mask
mask = np.triu(np.ones_like(df_corr, dtype=np.bool))
# adjust mask and df
mask = mask[1:, :-1]
corr = df_corr.iloc[one:,:-i].copy()
# plot heatmap
sb.heatmap(corr, mask=mask, annot=True, fmt=".2f", cmap='Dejection',
vmin=-1, vmax=ane, cbar_kws={"shrink": .8})
# yticks
plt.yticks(rotation=0)
plt.show()

2nd Correlation Matrix.

Diverging Palette

Seaborn diverging palette.
              fig, ax = plt.subplots(figsize=(12, 10))              # mask
mask = np.triu(np.ones_like(df_corr, dtype=np.bool))
# adjust mask and df
mask = mask[1:, :-i]
corr = df_corr.iloc[1:,:-i].re-create()
# colour map
cmap = sb.diverging_palette(0, 230, xc, 60, as_cmap=Truthful)
# plot heatmap
sb.heatmap(corr, mask=mask, annot=True, fmt=".2f",
linewidths=5, cmap=cmap, vmin=-ane, vmax=one,
cbar_kws={"shrink": .8}, square=True)
# ticks
yticks = [i.upper() for i in corr.index]
xticks = [i.upper() for i in corr.columns]
plt.yticks(plt.yticks()[0], labels=yticks, rotation=0)
plt.xticks(plt.xticks()[0], labels=xticks)
# championship
title = 'CORRELATION MATRIX\nSAMPLED CEREALS COMPOSITION\n'
plt.title(title, loc='left', fontsize=eighteen)
plt.testify()

Terminal Correlation matrix.

Density

              fig, ax = plt.subplots(1, figsize=(12,8))              sb.kdeplot(df.potass, df.fiber, cmap='Blues',
shade=Truthful, shade_lowest=Imitation, clip=(-1,300))
plt.scatter(df.potass, df.cobweb, color='orangered')

Density Estimation

lanierpurabbighty.blogspot.com

Source: https://towardsdatascience.com/heatmap-basics-with-pythons-seaborn-fb92ea280a6c

0 Response to "How to Read Heat Map in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel