当前位置:网站首页>Seaborn的使用
Seaborn的使用
2022-07-21 14:28:00 【樱花的浪漫】
1.5种主题风格
- darkgrid
- whitegrid
- dark
- white
- ticks
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)
sns.set_style("dark")
sinplot()
sns.set_style("white")
sinplot()
sns.set_style("ticks")
sinplot()
绘图细节设置
不要上面和右面的刻度
sinplot()
sns.despine()
设置刻度线离轴线的距离:
sns.violinplot(data)
sns.despine(offset=10)
隐藏刻度线:
sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True)
使用不同的风格:
with sns.axes_style("darkgrid"):
plt.subplot(211)
sinplot()
plt.subplot(212)
sinplot(-1)
设置线条大小
sns.set_context("paper")
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()
设置文字大小:
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sinplot()
2.调色板
调色板
- 颜色很重要
- color_palette()能传入任何Matplotlib所支持的颜色
- color_palette()不写参数则默认颜色
- set_palette()设置所有图的颜色
默认主题:
current_palette = sns.color_palette()
sns.palplot(current_palette)
6个默认的颜色循环主题: deep, muted, pastel, bright, dark, colorblind
圆形画板
当你有六个以上的分类要区分时,最简单的方法就是在一个圆形的颜色空间中画出均匀间隔的颜色(这样的色调会保持亮度和饱和度不变)。这是大多数的当他们需要使用比当前默认颜色循环中设置的颜色更多时的默认方案。
最常用的方法是使用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
成对颜色分布
使用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后缀
cubehelix_palette()调色板
色调线性变换
ight_palette() 和dark_palette()调用定制连续调色板
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);
3.单变量分析绘图
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x = np.random.normal(size=100)
sns.distplot(x, bins=20, kde=False)
数据分布情况
x = np.random.gamma(6, size=200)
sns.distplot(x, kde=False, fit=stats.gamma)
根据均值和协方差生成数据
mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])
df
观测两个变量之间的分布关系最好用散点图
sns.jointplot(x="x", y="y", data=df);
hex图
x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):
sns.jointplot(x=x, y=y, kind="hex", color="k")
iris = sns.load_dataset("iris")
sns.pairplot(iris)
回归分析绘图
iris = sns.load_dataset("iris")
sns.pairplot(iris)
regplot()和lmplot()都可以绘制回归关系,推荐regplot()
4. 多变量分析绘图
重叠是很常见的现象,但是重叠影响我观察数据的量了
加上小范围的偏移
盒图
- QR即统计学概念四分位距,第一/四分位与第三/四分位之间的距离
- N = 1.5IQR 如果一个值>Q3+N或 < Q1-N,则为离群点
组合
显示值的集中趋势可以用条形图
点图可以更好的描述变化差异
sns.pointplot(x="class", y="survived", hue="sex", data=titanic,
palette={"male": "g", "female": "m"},
markers=["^", "o"], linestyles=["-", "--"]);
宽形数据
多层面板分类图
eaborn.factorplot(x=None, y=None, hue=None, data=None, row=None, col=None, col_wrap=None, estimator=, ci=95, n_boot=1000, units=None, order=None, hue_order=None, row_order=None, col_order=None, kind='point', size=4, aspect=1, orient=None, color=None, palette=None, legend=True, legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, **kwargs)
Parameters:
x,y,hue 数据集变量 变量名
date 数据集 数据集名
row,col 更多分类变量进行平铺显示 变量名
col_wrap 每行的最高平铺数 整数
estimator 在每个分类中进行矢量到标量的映射 矢量
ci 置信区间 浮点数或None
n_boot 计算置信区间时使用的引导迭代次数 整数
units 采样单元的标识符,用于执行多级引导和重复测量设计 数据变量或向量数据
order, hue_order 对应排序列表 字符串列表
row_order, col_order 对应排序列表 字符串列表
kind : 可选:point 默认, bar 柱形图, count 频次, box 箱体, violin 提琴, strip 散点,swarm 分散点 size 每个面的高度(英寸) 标量 aspect 纵横比 标量 orient 方向 "v"/"h" color 颜色 matplotlib颜色 palette 调色板 seaborn颜色色板或字典 legend hue的信息面板 True/False legend_out 是否扩展图形,并将信息框绘制在中心右边 True/False share{x,y} 共享轴线 True/False
5.facetqrid
指定画图顺序
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print (ordered_days)
ordered_days = Categorical(['Thur', 'Fri', 'Sat', 'Sun'])
g = sns.FacetGrid(tips, row="day", row_order=ordered_days,
size=1.7, aspect=4,)
g.map(sns.boxplot, "total_bill");
使用matplotlib
pal = dict(Lunch="seagreen", Dinner="gray")
g = sns.FacetGrid(tips, hue="time", palette=pal, size=5)
g.map(plt.scatter, "total_bill", "tip", s=50, alpha=.7, linewidth=.5, edgecolor="white")
g.add_legend();
g = sns.FacetGrid(tips, hue="sex", palette="Set1", size=5, hue_kws={"marker": ["^", "v"]})
g.map(plt.scatter, "total_bill", "tip", s=100, linewidth=.5, edgecolor="white")
g.add_legend();
with sns.axes_style("white"):
g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, size=2.5)
g.map(plt.scatter, "total_bill", "tip", color="#334488", edgecolor="white", lw=.5);
g.set_axis_labels("Total bill (US Dollars)", "Tip");
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]);
g.fig.subplots_adjust(wspace=.02, hspace=.02);
#g.fig.subplots_adjust(left = 0.125,right = 0.5,bottom = 0.1,top = 0.9, wspace=.02, hspace=.02)
5.热度图
%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)
加入实际值
加入线条
设置颜色
去除色板
边栏推荐
- 同城订单同城送,爆单依旧得心应手!
- "New capabilities" of rongyun Super Group
- 设置背景颜色,背景范围等,雪碧图,渐变色,放射渐变
- 第2集 vmware虚拟机安装最牛B教程(13天)
- 技术团队:给代码评审发起者的4个建议
- leetcode 931. Minimum sum of descent path
- Vector container member function reserve() and iterator failure
- leetcode 1380. Lucky number in matrix
- haproxy2.6负载安装配置
- How to avoid the risk of mismatch between Ethernet interface and wiring
猜你喜欢
随机推荐
leetcode 310. 最小高度树
第3集 vmware虚拟机安装最牛B教程(14天)
DLL免杀技术探讨
网络连接工具大全
面试遇到的问题
Some tool modifications
leetcode 376.摆动序列
64.最小路径和
Vector container member function reserve() and iterator failure
Netcore——Middleware中间件(1)
Analysis on the characteristics of two-layer industrial switch
leetcode 1732.找到最高海拔
聚醚/聚丙烯酰胺-竣甲基/聚丙烯酰胺/粒状聚N-异丙基丙烯酰胺壳聚糖水凝胶的制备方法
β-环糊精衍生物接枝羟丙基壳聚糖水凝胶/羧基改性壳聚糖固载环糊精水凝胶微球的制备
过d盾asp webshell+冰蝎免杀马探讨
表格表单的样式
高度塌陷和清除浮动
Navicate 连接阿里云(两种方式及原理讲解)
leetcode 938. 二叉搜索树的范围和
Leetcode 539. 最小时间差