可视化库seaborn的风格和颜色以及热度图
2017-10-06 本文已影响0人
wong11
kaggle titanic数据
#年龄的分布
import pandas as pd
titanic = pd.read_csv('train.csv')
cols = ['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
titanic = titanic[cols].dropna()
import seaborn as sns
import matplotlib.pyplot as plt
sns.distplot(titanic['Age'])
plt.show()

import seaborn as sns
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
#%matplotlib inline是jupyter notebook里的命令, 意思是将那些用matplotlib绘制的图显示在页面里而不是弹出一个窗口
%matplotlib inline
def sinplot(flip=1):
#在区间0-14中找出100个点
x = np.linspace(0, 14, 100)
#画出6条不同的sin曲线
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()

#set()代表使用seaborn中的默认参数,或者说返回到原先默认的状态
sns.set()
sinplot()

seaborn的5种主题风格
darkgrid
whitegrid
dark
white
ticks
#风格whitegrid
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)

#风格dark
sns.set_style("dark")
sinplot()

#风格white
sns.set_style("white")
sinplot()

##风格ticks
sns.set_style("ticks")
sinplot()

sinplot()
#去掉上面和右边的边框
sns.despine()

#f, ax = plt.subplots()
sns.violinplot(data)
#offset设置图和轴线的距离
sns.despine(offset=10)

sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
#隐去左边框,可以通过true or false 来操作上,下,左,右边框
sns.despine(left=True)

#在多图作为子图拼接的过程中,可以使用with,with 中为相同风格
with sns.axes_style("darkgrid"):
plt.subplot(211)
sinplot()
#with 外为其他风格,这里sinplot(-1)取上图完全相反的风格
plt.subplot(212)
sinplot(-1)

图的背景的4种风格
paper
talk
poster
notebook
#先恢复默认
sns.set_context("paper")
#figsize调整图的尺寸
plt.figure(figsize=(8, 6))
sinplot()

sns.set_context("talk")
plt.figure(figsize=(8, 6))
sinplot()

sns.set_context("poster")
plt.figure(figsize=(8, 6))
sinplot()

#font_scale用来tiao调整图中字的大小,lines.linewidth调整线的粗细
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sinplot()

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set(rc={"figure.figsize": (6, 6)})
调色板
颜色很重要
color_palette()能传入任何Matplotlib所支持的颜色
color_palette()不写参数则默认颜色
set_palette()设置所有图的颜色
分类色板
##6个默认的颜色循环主题: deep, muted, pastel, bright, dark, colorblind
current_palette = sns.color_palette()
sns.palplot(current_palette)

圆形画板
当你有六个以上的分类要区分时,最简单的方法就是在一个圆形的颜色空间中画出均匀间隔的颜色(这样的色调会保持亮度和饱和度不变)。这是大多数的当他们需要使用比当前默认颜色循环中设置的颜色更多时的默认方案。
最常用的方法是使用hls的颜色空间,这是RGB值的一个简单转换。
sns.palplot(sns.color_palette("hls", 8))

data = np.random.normal(size=(20, 8)) + np.arange(8) / 2
sns.boxplot(data=data,palette=sns.color_palette("hls", 8))

hls_palette()函数来控制颜色的亮度和饱和
l-亮度 lightness
s-饱和 saturation
sns.palplot(sns.hls_palette(8, l=.7, s=.9))

##让颜色成对出现,例如浅蓝深蓝,浅绿深绿
sns.palplot(sns.color_palette("Paired",8))

使用xkcd颜色来命名颜色
xkcd包含了一套众包努力的针对随机RGB色的命名。产生了954个可以随时通过xdcd_rgb字典中调用的命名颜色。
plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)
plt.plot([0, 1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)
plt.plot([0, 1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3)

colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
sns.palplot(sns.xkcd_palette(colors))

连续色板
色彩随数据变换,比如数据越来越重要则颜色越来越深
sns.palplot(sns.color_palette("Blues"))

##如果想要翻转渐变,可以在面板名称中添加一个_r后缀
sns.palplot(sns.color_palette("BuGn_r"))

cubehelix_palette()调色板
色调线性变换
sns.palplot(sns.color_palette("cubehelix", 8))

sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75))

sns.palplot(sns.cubehelix_palette(8, start=.75, rot=-.150))

light_palette() 和dark_palette()调用定制连续调色板
sns.palplot(sns.light_palette("green"))

sns.palplot(sns.dark_palette("purple"))

sns.palplot(sns.light_palette("navy", reverse=True))

x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T
pal = sns.dark_palette("green", as_cmap=True)
sns.kdeplot(x, y, cmap=pal);

sns.palplot(sns.light_palette((210, 90, 60), input="husl"))

热度图
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np;
np.random.seed(0)
import seaborn as sns;
sns.set()
uniform_data = np.random.rand(3, 3)
print (uniform_data)
heatmap = sns.heatmap(uniform_data)
##结果
[[ 0.5488135 0.71518937 0.60276338]
[ 0.54488318 0.4236548 0.64589411]
[ 0.43758721 0.891773 0.96366276]]

##vmin=0.2, vmax=0.5代表最小最大的取值范围
ax = sns.heatmap(uniform_data, vmin=0.2, vmax=0.5)

##center=0代表colorbar中心的值
normal_data = np.random.randn(3, 3)
print (normal_data)
ax = sns.heatmap(normal_data, center=0)
###结果:
[[ 1.26611853 -0.50587654 2.54520078]
[ 1.08081191 0.48431215 0.57914048]
[-0.18158257 1.41020463 -0.37447169]]

下面用一组航班数据做演示:
flights = sns.load_dataset("flights")
##flights = flights.pivot("month", "year", "passengers")代表横轴纵轴和值
flights = flights.pivot("month", "year", "passengers")
print (flights)
ax = sns.heatmap(flights)


##annot=True,把值添加进来,fmt="d",一种比较清晰的字体格式,
##默认格式是科学计数法数字太长,容易出现乱码
ax = sns.heatmap(flights, annot=True,fmt="d")

##linewidths=.5 格子之间的间距
ax = sns.heatmap(flights, linewidths=.5)

##设定颜色区间
ax = sns.heatmap(flights, cmap="YlGnBu")

##隐藏colorbar
ax = sns.heatmap(flights, cbar=False)

昨晚发现一只勤劳的小蜜蜂把seaborn官方文档大部分都学习了一边,剩余的一些部分直接转载了:
第三章 分布数据集的可视化:
https://zhuanlan.zhihu.com/p/27570774
第四章 线性关系的可视化
https://zhuanlan.zhihu.com/p/27593869
第五章 分类数据的绘制
https://zhuanlan.zhihu.com/p/27683042
第六章 绘制数据网格
https://zhuanlan.zhihu.com/p/27816821