【案例+源码】数据可视化之统计绘图-Seaborn全套教程_different cubehelix palettes

在这里插入图片描述

“”"
Lineplot from a wide-form dataset

“”"

sns.set(style=“whitegrid”)

rs = np.random.RandomState(365)
values = rs.randn(365, 4).cumsum(axis=0)
dates = pd.date_range(“1 1 2016”, periods=365, freq=“D”)
data = pd.DataFrame(values, dates, columns=[“A”, “B”, “C”, “D”])
data = data.rolling(7).mean()

sns.lineplot(data=data, palette=“tab10”, linewidth=2.5)

在这里插入图片描述

relplot

这是一个图形级别的函数,它用散点图和线图两种常用的手段来表现统计关系。

“”"
Line plots on multiple facets

“”"

sns.set(style=“ticks”)

dots = sns.load_dataset(“dots”)

Define a palette to ensure that colors will be

shared across the facets

palette = dict(zip(dots.coherence.unique(),
sns.color_palette(“rocket_r”, 6)))

Plot the lines on two facets

sns.relplot(x=“time”, y=“firing_rate”,
hue=“coherence”, size=“choice”, col=“align”,
size_order=[“T1”, “T2”], palette=palette,
height=5, aspect=.75, facet_kws=dict(sharex=False),
kind=“line”, legend=“full”, data=dots)

在这里插入图片描述

boxplot

箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。它能显示出一组数据的最大值、最小值、中位数及上下四分位数。

“”"
Grouped boxplots

“”"
sns.set(style=“ticks”, palette=“pastel”)

Load the example tips dataset

tips = sns.load_dataset(“tips”)

Draw a nested boxplot to show bills by day and time

sns.boxplot(x=“day”, y=“total_bill”,
hue=“smoker”, palette=[“m”, “g”],
data=tips)
sns.despine(offset=10, trim=True)

在这里插入图片描述

violinplot

violinplot与boxplot扮演类似的角色,它显示了定量数据在一个(或多个)分类变量的多个层次上的分布,这些分布可以进行比较。不像箱形图中所有绘图组件都对应于实际数据点,小提琴绘图以基础分布的核密度估计为特征。

“”"
Violinplots with observations

“”"

sns.set()

Create a random dataset across several variables

rs = np.random.RandomState(0)
n, p = 40, 8
d = rs.normal(0, 2, (n, p))
d += np.log(np.arange(1, p + 1)) * -5 + 10

Use cubehelix to get a custom sequential palette

pal = sns.cubehelix_palette(p, rot=-.5, dark=.3)

Show each distribution with both violins and points

sns.violinplot(data=d, palette=pal, inner=“points”)

在这里插入图片描述

“”"
Grouped violinplots with split violins

“”"
sns.set(style=“whitegrid”, palette=“pastel”, color_codes=True)

Load the example tips dataset

tips = sns.load_dataset(“tips”)

Draw a nested violinplot and split the violins for easier comparison

sns.violinplot(x=“day”, y=“total_bill”, hue=“smoker”,
split=True, inner=“quart”,
palette={“Yes”: “y”, “No”: “b”},
data=tips)
sns.despine(left=True)

在这里插入图片描述

“”"
Violinplot from a wide-form dataset

“”"

sns.set(style=“whitegrid”)

Load the example dataset of brain network correlations

df = sns.load_dataset(“brain_networks”, header=[0, 1, 2], index_col=0)

Pull out a specific subset of networks

used_networks = [1, 3, 4, 5, 6, 7, 8, 11, 12, 13, 16, 17]
used_columns = (df.columns.get_level_values(“network”)
.astype(int)
.isin(used_networks))
df = df.loc[:, used_columns]

Compute the correlation matrix and average over networks

corr_df = df.corr().groupby(level=“network”).mean()
corr_df.index = corr_df.index.astype(int)
corr_df = corr_df.sort_index().T

Set up the matplotlib figure

f, ax = plt.subplots(figsize=(11, 6))

Draw a violinplot with a narrower bandwidth than the default

sns.violinplot(data=corr_df, palette=“Set3”, bw=.2, cut=1, linewidth=1)

Finalize the figure

ax.set(ylim=(-.7, 1.05))
sns.despine(left=True, bottom=True)

在这里插入图片描述

heatmap热力图

利用热力图可以看数据表里多个特征两两的相似度。

“”"
Annotated heatmaps

“”"
sns.set()

Load the example flights dataset and conver to long-form

flights_long = sns.load_dataset(“flights”)
flights = flights_long.pivot(“month”, “year”, “passengers”)

Draw a heatmap with the numeric values in each cell

f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True, fmt=“d”, linewidths=.5, ax=ax)

在这里插入图片描述

“”"
Plotting a diagonal correlation matrix

“”"
from string import ascii_letters

sns.set(style=“white”)

Generate a large random dataset

rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
columns=list(ascii_letters[26:]))

Compute the correlation matrix

corr = d.corr()

Generate a mask for the upper triangle

mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

Set up the matplotlib figure

f, ax = plt.subplots(figsize=(11, 9))

Generate a custom diverging colormap

cmap = sns.diverging_palette(220, 10, as_cmap=True)

Draw the heatmap with the mask and correct aspect ratio

sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
square=True, linewidths=.5, cbar_kws={“shrink”: .5})

在这里插入图片描述

jointplot

用于2个变量的画图

“”"
Joint kernel density estimate

“”"
sns.set(style=“white”)

Generate a random correlated bivariate dataset

rs = np.random.RandomState(5)
mean = [0, 0]
cov = [(1, .5), (.5, 1)]
x1, x2 = rs.multivariate_normal(mean, cov, 500).T
x1 = pd.Series(x1, name=“ X _ 1 X\_1 X_1”)
x2 = pd.Series(x2, name=“ X _ 2 X\_2 X_2”)

Show the joint distribution using kernel density estimation

g = sns.jointplot(x1, x2, kind=“kde”, height=7, space=0)

在这里插入图片描述
pos_id=img-FBBVohhM-1694680791085)

HexBin图

直方图的双变量类似物被称为“hexbin”图,因为它显示了落在六边形仓内的观测数。该图适用于较大的数据集。

“”"
Hexbin plot with marginal distributions

“”"
sns.set(style=“ticks”)

rs = np.random.RandomState(11)
x = rs.gamma(2, size=1000)
y = -.5 * x + rs.normal(size=1000)

sns.jointplot(x, y, kind=“hex”, color=“#4CB391”)

在这里插入图片描述

“”"
Linear regression with marginal distributions

“”"

sns.set(style=“darkgrid”)

tips = sns.load_dataset(“tips”)
g = sns.jointplot(“total_bill”, “tip”, data=tips, kind=“reg”,
xlim=(0, 60), ylim=(0, 12), color=“m”, height=7)

在这里插入图片描述

barplot(条形图)

条形图表示数值变量与每个矩形高度的中心趋势的估计值,并使用误差线提供关于该估计值附近的不确定性的一些指示。

“”"
Horizontal bar plots

“”"
sns.set(style=“whitegrid”)

Load the example car crash dataset

crashes = sns.load_dataset(“car_crashes”).sort_values(“total”, ascending=False)

Initialize the matplotlib figure

f, ax = plt.subplots(figsize=(6, 15))

Plot the total crashes

sns.set_color_codes(“pastel”)
sns.barplot(x=“total”, y=“abbrev”, data=crashes,
label=“Total”, color=“b”)

Plot the crashes where alcohol was involved

sns.set_color_codes(“muted”)
sns.barplot(x=“alcohol”, y=“abbrev”, data=crashes,
label=“Alcohol-involved”, color=“b”)

Add a legend and informative axis label

ax.legend(ncol=2, loc=“lower right”, frameon=True)
ax.set(xlim=(0, 24), ylabel=“”,
xlabel=“Automobile collisions per billion miles”)
sns.despine(left=True, bottom=True)

在这里插入图片描述

catplot

分类图表的接口,通过指定kind参数可以画出下面的八种图

stripplot() 分类散点图

swarmplot() 能够显示分布密度的分类散点图

boxplot() 箱图

violinplot() 小提琴图

boxenplot() 增强箱图

pointplot() 点图

barplot() 条形图

countplot() 计数图

“”"
Grouped barplots

“”"
sns.set(style=“whitegrid”)

Load the example Titanic dataset

titanic = sns.load_dataset(“titanic”)

Draw a nested barplot to show survival for class and sex

g = sns.catplot(x=“class”, y=“survived”, hue=“sex”, data=titanic,
height=6, kind=“bar”, palette=“muted”)
g.despine(left=True)
g.set_ylabels(“survival probability”)

在这里插入图片描述

“”"
Plotting a three-way ANOVA

“”"

sns.set(style=“whitegrid”)

Load the example exercise dataset

df = sns.load_dataset(“exercise”)

Draw a pointplot to show pulse as a function of three categorical factors

g = sns.catplot(x=“time”, y=“pulse”, hue=“kind”, col=“diet”,
capsize=.2, palette=“YlGnBu_d”, height=6, aspect=.75,
kind=“point”, data=df)
g.despine(left=True)

在这里插入图片描述

pointplot

点图代表散点图位置的数值变量的中心趋势估计,并使用误差线提供关于该估计的不确定性的一些指示。点图可能比条形图更有用于聚焦一个或多个分类变量的不同级别之间的比较。他们尤其善于表现交互作用:一个分类变量的层次之间的关系如何在第二个分类变量的层次之间变化。连接来自相同色调等级的每个点的线允许交互作用通过斜率的差异进行判断,这比对几组点或条的高度比较容易。

“”"
Conditional means with observations

“”"
sns.set(style=“whitegrid”)
iris = sns.load_dataset(“iris”)

“Melt” the dataset to “long-form” or “tidy” representation

iris = pd.melt(iris, “species”, var_name=“measurement”)

Initialize the figure

f, ax = plt.subplots()
sns.despine(bottom=True, left=True)

Show each observation with a scatterplot

sns.stripplot(x=“value”, y=“measurement”, hue=“species”,
data=iris, dodge=True, jitter=True,
alpha=.25, zorder=1)

Show the conditional means

sns.pointplot(x=“value”, y=“measurement”, hue=“species”,
data=iris, dodge=.532, join=False, palette=“dark”,
markers=“d”, scale=.75, ci=None)

Improve the legend

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[3:], labels[3:], title=“species”,
handletextpad=0, columnspacing=1,
loc=“lower right”, ncol=3, frameon=True)

在这里插入图片描述

scatterplot(散点图)

“”"
Scatterplot with categorical and numerical semantics

“”"
sns.set(style=“whitegrid”)

Load the example iris dataset

diamonds = sns.load_dataset(“diamonds”)

Draw a scatter plot while assigning point colors and sizes to different

variables in the dataset

f, ax = plt.subplots(figsize=(6.5, 6.5))
sns.despine(f, left=True, bottom=True)
clarity_ranking = [“I1”, “SI2”, “SI1”, “VS2”, “VS1”, “VVS2”, “VVS1”, “IF”]
sns.scatterplot(x=“carat”, y=“price”,
hue=“clarity”, size=“depth”,
palette=“ch:r=-.2,d=.3_r”,
hue_order=clarity_ranking,
sizes=(1, 8), linewidth=0,
data=diamonds, ax=ax)

在这里插入图片描述

boxenplot(增强箱图)

“”"
Plotting large distributions

“”"
sns.set(style=“whitegrid”)

diamonds = sns.load_dataset(“diamonds”)
clarity_ranking = [“I1”, “SI2”, “SI1”, “VS2”, “VS1”, “VVS2”, “VVS1”, “IF”]

sns.boxenplot(x=“clarity”, y=“carat”,
color=“b”, order=clarity_ranking,
scale=“linear”, data=diamonds)

在这里插入图片描述

Scatterplot(散点图)

“”"
Scatterplot with continuous hues and sizes

“”"

sns.set()

Load the example iris dataset

planets = sns.load_dataset(“planets”)

cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
ax = sns.scatterplot(x=“distance”, y=“orbital_period”,
hue=“year”, size=“mass”,
palette=cmap, sizes=(10, 200),
data=planets)

在这里插入图片描述

“”"
Scatterplot with marginal ticks

“”"
sns.set(style=“white”, color_codes=True)

Generate a random bivariate dataset

rs = np.random.RandomState(9)
mean = [0, 0]
cov = [(1, 0), (0, 2)]
x, y = rs.multivariate_normal(mean, cov, 100).T

Use JointGrid directly to draw a custom plot

grid = sns.JointGrid(x, y, space=0, height=6, ratio=50)
grid.plot_joint(plt.scatter, color=“g”)
grid.plot_marginals(sns.rugplot, height=1, color=“g”)

在这里插入图片描述

PairGrid

用于绘制数据集中成对关系的子图网格。

“”"
Paired density and scatterplot matrix

“”"

sns.set(style=“white”)

df = sns.load_dataset(“iris”)

g = sns.PairGrid(df, diag_sharey=False)
g.map_lower(sns.kdeplot)
g.map_upper(sns.scatterplot)
g.map_diag(sns.kdeplot, lw=3)

在这里插入图片描述

“”"
Paired categorical plots

“”"
sns.set(style=“whitegrid”)

Load the example Titanic dataset

titanic = sns.load_dataset(“titanic”)

Set up a grid to plot survival probability against several variables

g = sns.PairGrid(titanic, y_vars=“survived”,
x_vars=[“class”, “sex”, “who”, “alone”],
height=5, aspect=.5)

Draw a seaborn pointplot onto each Axes

g.map(sns.pointplot, scale=1.3, errwidth=4, color=“xkcd:plum”)
g.set(ylim=(0, 1))
sns.despine(fig=g.fig, left=True)

在这里插入图片描述

residplot

线性回归残差图

“”"
Plotting model residuals

“”"

sns.set(style=“whitegrid”)

Make an example dataset with y ~ x

rs = np.random.RandomState(7)
x = rs.normal(2, 1, 75)
y = 2 + 1.5 * x + rs.normal(0, 2, 75)

Plot the residuals after fitting a linear model

sns.residplot(x, y, lowess=True, color=“g”)

在这里插入图片描述

“”"
Scatterplot with varying point sizes and hues

“”"
sns.set(style=“white”)

Load the example mpg dataset

mpg = sns.load_dataset(“mpg”)

Plot miles per gallon against horsepower with other semantics

sns.relplot(x=“horsepower”, y=“mpg”, hue=“origin”, size=“weight”,
sizes=(40, 400), alpha=.5, palette=“muted”,
height=6, data=mpg)

在这里插入图片描述

swarmplot

能够显示分布密度的分类散点图

“”"
Scatterplot with categorical variables

“”"

sns.set(style=“whitegrid”, palette=“muted”)

Load the example iris dataset

iris = sns.load_dataset(“iris”)

“Melt” the dataset to “long-form” or “tidy” representation

iris = pd.melt(iris, “species”, var_name=“measurement”)

Draw a categorical scatterplot to show each observation

sns.swarmplot(x=“measurement”, y=“value”, hue=“species”,
palette=[“r”, “c”, “y”], data=iris)

在这里插入图片描述

pairplot

变量关系组图

“”"
Scatterplot Matrix

“”"

sns.set(style=“ticks”)

df = sns.load_dataset(“iris”)
sns.pairplot(df, hue=“species”)

在这里插入图片描述

clustermap

聚集图

“”"
Discovering structure in heatmap data

“”"

sns.set()

Load the brain networks example dataset

df = sns.load_dataset(“brain_networks”, header=[0, 1, 2], index_col=0)

Select a subset of the networks

used_networks = [1, 5, 6, 7, 8, 12, 13, 17]
used_columns = (df.columns.get_level_values(“network”)
.astype(int)
.isin(used_networks))
df = df.loc[:, used_columns]

Create a categorical palette to identify the networks

network_pal = sns.husl_palette(8, s=.45)
network_lut = dict(zip(map(str, used_networks), network_pal))

Convert the palette to vectors that will be drawn on the side of the matrix

networks = df.columns.get_level_values(“network”)
network_colors = pd.Series(networks, index=df.columns).map(network_lut)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Go语言工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

mns).map(network_lut)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Go语言工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-CO3Ur14B-1713002923250)]
[外链图片转存中…(img-FE9jqi08-1713002923250)]
[外链图片转存中…(img-deOfuxC9-1713002923251)]
[外链图片转存中…(img-wvTdeUsE-1713002923251)]
[外链图片转存中…(img-Z9bqtXpY-1713002923252)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-ml9wt1bj-1713002923253)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 目录 1. 目录 2 2. 绘图函数Plotting functions 4 2.1. 可视化统计关系Visualizing statistical relationships 4 2.1.1. 用散点图联系变量Relating variables with scatter plots 4 2.1.2. 强调线条图的连续性Emphasizing continuity with line plots 10 2.1.3. 显示与切面的多个关系Showing multiple relationships with facets 21 2.2. 分类数据绘图Plotting with categorical data 24 2.2.1. 分类散点图Categorical scatterplots 26 2.2.2. 分类观测值分布Distributions of observations within categories 31 2.2.3. 分类统计估计Statistical estimation within categories 37 2.2.4. 对“wide-form”数据作图Plotting “wide-form” data 41 2.2.5. 显示与facet的多个关系Showing multiple relationships with facets 43 2.3. 可视化数据集的分布Visualizing the distribution of a dataset 44 2.3.1. 绘制单变量分布Plotting univariate distributions 45 2.3.2. 绘制二元分布Plotting bivariate distributions 51 2.3.3. 在数据集中可视化成对关系Visualizing pairwise relationships in a dataset 55 2.4. 可视化线性关系Visualizing linear relationships 57 2.4.1. 函数绘制线性模型Functions to draw linear regression models 58 2.4.2. 拟合不同种类的模型Fitting different kinds of models 61 2.4.3. 在其他变量上的情况Conditioning on other variables 68 2.4.4. 控制图表的大小和形状Controlling the size and shape of the plot 71 2.4.5. 在其他上下文中绘制回归图Plotting a regression in other contexts 73 3. 多图网格Multi-plot grids 76 3.1. 构建结构化的多图网格Building structured multi-plot grids 76 3.2. 有条件的小倍数Conditional small multiples 77 3.3. 使用定制函数Using custom functions 86 3.4. 绘制成对的数据关系Plotting pairwise data relationships 90 4. 绘图美学Plot aesthetics 99 4.1. 控制图表美学Controlling figure aesthetics 99 4.1.1. Seaborn图表风格Seaborn figure styles 101 4.1.2. 删除轴上的小凸起Removing axes spines 104 4.1.3. 临时设置图表样式Temporarily setting figure style 105 4.1.4. 覆盖Seaborn样式的元素Overriding elements of the seaborn styles 106 4.1.5. 缩放图表元素Scaling plot elements 108 4.2. 选择调色板Choosing color palettes 111 4.2.1. 创建颜色调色板Building color palettes 111 4.2.2. 定性调色板Qualitative color palettes 112 4.2.3. 连续调色板Sequential color palettes 116 4.2.4. 不同颜色的调色板Diverging color palettes 122 4.2.5. 设置默认调色板Setting the default color palette 124 5. 教程中的数据集 125

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值