@[toc]
熱力圖 heatmap
熱力圖就是將矩形數(shù)據(jù)繪制為顏色編碼矩陣。
numpy數(shù)組繪制熱力圖
import numpy as np; #np.random.seed(0)
import seaborn as sns; #sns.set()
uniform_data = np.random.rand(10, 12)
#print(uniform_data)
ax = sns.heatmap(uniform_data)
在這里插入圖片描述
更改默認(rèn)的colormap范圍
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
在這里插入圖片描述
使用發(fā)散色圖繪制以 0 為中心的數(shù)據(jù)的熱力圖
normal_data = np.random.randn(10, 12)
ax = sns.heatmap(normal_data, center=0)
在這里插入圖片描述
使用特定的行和列標(biāo)簽繪制 dataframe
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights)
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
month
Jan 112 115 145 171 196 204 242 284 315 340 360 417
Feb 118 126 150 180 196 188 233 277 301 318 342 391
Mar 132 141 178 193 236 235 267 317 356 362 406 419
Apr 129 135 163 181 235 227 269 313 348 348 396 461
May 121 125 172 183 229 234 270 318 355 363 420 472
Jun 135 149 178 218 243 264 315 374 422 435 472 535
Jul 148 170 199 230 264 302 364 413 465 491 548 622
Aug 148 170 199 242 272 293 347 405 467 505 559 606
Sep 136 158 184 209 237 259 312 355 404 404 463 508
Oct 119 133 162 191 211 229 274 306 347 359 407 461
Nov 104 114 146 172 180 203 237 271 305 310 362 390
Dec 118 140 166 194 201 229 278 306 336 337 405 432
在這里插入圖片描述
使用整數(shù)格式的數(shù)字值注釋每個(gè)小單元格
ax = sns.heatmap(flights, annot=True, fmt="d")
在這里插入圖片描述
在每個(gè)單元格之間添加線
ax = sns.heatmap(flights, linewidths=.5)
在這里插入圖片描述
使用不同的 colormap
ax = sns.heatmap(flights, cmap="YlGnBu")
在這里插入圖片描述
將 colormap 置于特定值的中心
ax = sns.heatmap(flights, center=flights.loc["Jan", 1955])
在這里插入圖片描述
繪制每個(gè)其他列標(biāo)簽,而不繪制行標(biāo)簽
data = np.random.randn(50, 20)
ax = sns.heatmap(data, xticklabels=2, yticklabels=False)
在這里插入圖片描述
在不同的坐標(biāo)軸方向繪制顏色條
grid_kws = {"height_ratios": (.9, .05), "hspace": .4}
f, (ax, cbar_ax) = plt.subplots(2, gridspec_kw=grid_kws)
ax = sns.heatmap(flights, ax=ax,
cbar_ax=cbar_ax,
cbar_kws={"orientation": "horizontal"})
在這里插入圖片描述
使用遮罩繪制矩陣中的一部分
corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)
在這里插入圖片描述
聚類熱圖 clustermap
import seaborn as sns; sns.set(color_codes=True)
iris = sns.load_dataset("iris")
species = iris.pop("species")
g = sns.clustermap(iris)
在這里插入圖片描述
使用不同的相似性指標(biāo)
g = sns.clustermap(iris, metric="correlation")
在這里插入圖片描述
使用不同的色彩映射并忽略色彩映射限制中的異常值
g = sns.clustermap(iris, cmap="mako", robust=True)
在這里插入圖片描述
添加彩色標(biāo)簽
lut = dict(zip(species.unique(), "rbg"))
row_colors = species.map(lut)
g = sns.clustermap(iris, row_colors=row_colors)
在這里插入圖片描述
散點(diǎn)圖 scattermap
繪制一個(gè)兩個(gè)變量的簡(jiǎn)單散點(diǎn)圖
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
ax = sns.scatterplot(x='total_bill',y='tip',data=tips)
在這里插入圖片描述
通過其他的變量分組并且用不同的顏色展示分組
ax = sns.scatterplot(x='total_bill',y='tip',hue='time',data=tips)
在這里插入圖片描述
一個(gè)類別變量不同的大小,用不同的顏色
ax = sns.scatterplot(x='total_bill',y='tip',hue='day',size='smoker',
palette='Set2',data=tips)
在這里插入圖片描述
箱型圖 boxplot
箱形圖(或盒須圖)以一種利于變量之間比較或不同分類變量層次之間比較的方式來展示定量數(shù)據(jù)的分布。圖中矩形框顯示數(shù)據(jù)集的上下四分位數(shù),而矩形框中延伸出的線段(觸須)則用于顯示其余數(shù)據(jù)的分布位置,剩下超過上下四分位間距的數(shù)據(jù)點(diǎn)則被視為“異常值”。
繪制一個(gè)單獨(dú)的橫向箱型圖
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"])
在這里插入圖片描述
根據(jù)分類變量分組繪制一個(gè)縱向的箱型圖
ax = sns.boxplot(x="day", y="total_bill", data=tips)
在這里插入圖片描述
使用 swarmplot() 展示箱型圖頂部的數(shù)據(jù)點(diǎn)
ax = sns.boxplot(x="day", y="total_bill", data=tips)
ax = sns.swarmplot(x="day", y="total_bill", data=tips, color=".25")
在這里插入圖片描述
提琴圖 violinplot
小提琴圖的功能與箱型圖類似。 它顯示了一個(gè)(或多個(gè))分類變量多個(gè)屬性上的定量數(shù)據(jù)的分布,從而可以比較這些分布。與箱形圖不同,其中所有繪圖單元都與實(shí)際數(shù)據(jù)點(diǎn)對(duì)應(yīng),小提琴圖描述了基礎(chǔ)數(shù)據(jù)分布的核密度估計(jì)。
小提琴圖可以是一種單次顯示多個(gè)數(shù)據(jù)分布的有效且有吸引力的方式,但請(qǐng)記住,估計(jì)過程受樣本大小的影響,相對(duì)較小樣本的小提琴可能看起來非常平滑,這種平滑具有誤導(dǎo)性。
繪制一個(gè)單獨(dú)的橫向小提琴圖
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x=tips["total_bill"])
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
.. ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2
在這里插入圖片描述
根據(jù)分類變量分組繪制一個(gè)縱向的小提琴圖
ax = sns.violinplot(x="day", y="total_bill", data=tips)
在這里插入圖片描述