In [ ]:
%matplotlib inline
In [ ]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
In [ ]:
iris = sns.load_dataset("iris")
flights = sns.load_dataset("flights")
networks = sns.load_dataset("brain_networks", index_col=0, header=[0, 1, 2])
In [ ]:
flights.head()
In [ ]:
flights_rect = flights.pivot("month", "year", "passengers")
flights_rect = flights_rect.ix[flights.month.iloc[:12]]
In [ ]:
flights_rect.head()
In [ ]:
sns.heatmap(flights_rect);
In [ ]:
sns.heatmap(flights_rect, annot=True, fmt="d");
In [ ]:
network_corr = networks.iloc[:, :12].corr()
sns.heatmap(network_corr);
In [ ]:
sns.heatmap(network_corr, vmax=.8, square=True);
In [ ]:
sns.heatmap(flights_rect, center=flights_rect.loc["January", 1955]);
In [ ]:
f = plt.figure(figsize=(7, 9))
gs = plt.GridSpec(15, 1)
hist_ax = f.add_subplot(gs[:5])
yearly_flights = flights_rect.sum(axis=0)
hist_ax.bar(range(12), yearly_flights, 1, ec="w", lw=2, color=".3")
hist_ax.set(xticks=[], ylabel="flights")
map_ax = f.add_subplot(gs[5:-2])
bar_ax = f.add_subplot(gs[-1])
sns.heatmap(flights_rect, cmap="BuGn", ax=map_ax,
cbar_ax=bar_ax, cbar_kws={"orientation": "horizontal"})
bar_ax.set(xlabel="flights");
In [ ]:
cg = sns.clustermap(flights_rect)
In [ ]:
cg = sns.clustermap(flights_rect, col_cluster=False)
In [ ]:
cg = sns.clustermap(flights_rect, standard_scale=1)
In [ ]:
cg = sns.clustermap(flights_rect, standard_scale=0)
We could also normalize the rows by their $Z$-score, which subtracts the mean and divides by the standard deviation of each column, thus standardizing them to have 0 mean and a variance of 1. This is helpful for easily seeing which values are greater than the mean, and which are smaller.
In [ ]:
cg = sns.clustermap(flights_rect, z_score=1)
In [ ]:
col_colors = sns.color_palette('Greens', n_colors=flights_rect.shape[1])
cg = sns.clustermap(flights_rect, standard_scale=True, col_colors=col_colors)
In [ ]:
col_colors = sns.color_palette('Greens', n_colors=flights_rect.shape[1])
season_colors = {'Winter': sns.color_palette('PuBu', n_colors=3),
'Spring': sns.color_palette('YlGn', n_colors=3),
'Summer': sns.color_palette('YlOrBr', n_colors=3),
'Fall': sns.color_palette('OrRd', n_colors=3)}
month_colors = {'January': season_colors['Winter'][1],
'February': season_colors['Winter'][2],
'March': season_colors['Spring'][0],
'April': season_colors['Spring'][1],
'May': season_colors['Spring'][2],
'June': season_colors['Summer'][0],
'July': season_colors['Summer'][1],
'August': season_colors['Summer'][2],
'September': season_colors['Fall'][0],
'October': season_colors['Fall'][1],
'November': season_colors['Fall'][2],
'December': season_colors['Winter'][0]}
row_colors = pd.Series(flights_rect.index).map(month_colors)
cg = sns.clustermap(flights_rect, standard_scale=True, col_colors=col_colors, row_colors=row_colors)
In [ ]:
pivot_kws = dict(index="month", columns="year", values="passengers")
cg = sns.clustermap(flights, pivot_kws=pivot_kws, standard_scale=1)
In [ ]:
data2d = np.random.randn(32).reshape(4, 8)
data2d[:2, :2] += 2
mask = data2d > 1
sns.clustermap(data2d, mask=mask)
In [ ]:
cg.dendrogram_col.reordered_ind