Seaborn的使用技巧有哪些

发布时间:2021-12-27 13:56:51 作者:iii
来源:亿速云 阅读:143

本篇内容介绍了“Seaborn的使用技巧有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

基线图

本文中的脚本在Jupyter笔记本中的python3.8.3中进行了测试。

让我们使用Seaborn内置的penguins数据集作为样本数据:

# 导入包 import matplotlib.pyplot as plt import seaborn as sns  # 导入数据 df = sns.load_dataset('penguins').rename(columns={'sex': 'gender'}) df
Seaborn的使用技巧有哪些

我们将使用默认图表设置构建标准散点图,以将其用作基线:

# 图 sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender')
Seaborn的使用技巧有哪些

我们将看到这个图如何随着每一个技巧而改变。

技巧

你将看到,前两个技巧用于单个绘图,而其余四个技巧用于更改所有图表的默认设置。

技巧1:分号

你有没有注意到在上一个图中,文本输出就在图表的正上方?抑制此文本输出的一个简单方法是在绘图末尾使用;。

#   图 sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender');
Seaborn的使用技巧有哪些

只需在代码末尾添加;就可以得到更清晰的输出。

技巧2:plt.figure()

绘图通常可以从调整大小中获益。如果我们想调整大小,我们可以这样做:

# 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender');
Seaborn的使用技巧有哪些

当我们调整大小时,图例移到了左上角。让我们将图例移到图表之外,这样它就不会意外地覆盖数据点:

# 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1));
Seaborn的使用技巧有哪些

如果你想知道如何知道figsize()或bbox_to_anchor()使用什么数的字组合,则需要尝试哪些数字最适合绘图。

技巧3:sns.set_style()

如果不喜欢默认样式,此函数有助于更改绘图的整体样式。这包括轴的颜色和背景。让我们将样式更改为whitegrid,并查看打印外观如何更改:

# 更改默认样式 sns.set_style('whitegrid')  # 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1));
Seaborn的使用技巧有哪些

这里还有一些其他的选择可以尝试:“darkgrid”、“dark”和“ticks”来找到你更喜欢的那个。

技巧4:sns.set_context()

在前面的图中,标签尺寸看起来很小。如果不喜欢默认设置,我们使用sns.set_context()可以更改上下文参数。

我使用这个函数主要是为了控制绘图中标签的默认字体大小。通过更改默认值,我们可以节省时间,而不必为单个绘图的不同元素(例如轴标签、标题、图例)调整字体大小。让我们把上下文改成“talk”,再看看图:

# 默认上下文更改 sns.set_context('talk')  # 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));
Seaborn的使用技巧有哪些

它更容易辨认,不是吗?另一个可以尝试的选项是:“poster”,这将增加默认大小甚至更多。

技巧5:sns.set_palette()

如果你想将默认调色板自定义为你喜欢的颜色组合,此功能非常方便。我们可以使用Matplotlib中的彩色映射。这里是从颜色库中选择的。让我们将调色板更改为“rainbow”并再次查看该图:

# 更改默认调色板 sns.set_palette('rainbow')  # 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));
Seaborn的使用技巧有哪些

如果找不到你喜欢的Matplotlib颜色映射,可以手动选择颜色来创建自己独特的调色板。  创建自己调色板的一种方法是将颜色名称列表传递给函数,如下例所示。这个链接是颜色名称列表:https://matplotlib.org/3.1.0/gallery/color/named_colors.html。

# 更改默认调色板 sns.set_palette(['green', 'purple', 'red'])  # 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));
Seaborn的使用技巧有哪些

如果颜色名称不能很好地捕捉到你所追求的,你可以使用十六进制颜色构建自己的调色板来访问更广泛的选项(超过1600万种颜色!)。这里是我最喜欢的资源,可以找到一个十六进制的自定义调色板。我们来看一个例子:

# 更改默认调色板 sns.set_palette(['#62C370', '#FFD166', '#EF476F'])  # 图 plt.figure(figsize=(9, 5)) sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',                  alpha=0.7, hue='species', size='gender') plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));
Seaborn的使用技巧有哪些

技巧6:sns.set()

从前面的三个技巧中,我希望你能找到你最喜欢的组合(在某些情况下,它可能会保留默认设置)。如果我们要更新图表的默认设置,最好是在导入可视化软件包之后再更新。这意味着我们在脚本的开头会有这样一个片段:

# 导入包 import matplotlib.pyplot as plt import seaborn as sns  # 更改默认值 sns.set_style('whitegrid') sns.set_context('talk') sns.set_palette('rainbow')

更新上面的多个默认值可以用sns.set(). 以下是同一代码的简洁版本:

# 导入包 import matplotlib.pyplot as plt import seaborn as sns  # 更改默认值 sns.set(style='whitegrid', context='talk', palette='rainbow')

这是六个技巧。以下是调整前后的图对比:

Seaborn的使用技巧有哪些

“Seaborn的使用技巧有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Python的使用技巧有哪些
  2. 使用Python的技巧有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

seaborn

上一篇:java 8新特性StampedLock是如何解决同步问题的

下一篇:Android如何自定View实现滑动验证效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》