5

Seaborn的6个简单技巧

 3 years ago
source link: https://panchuang.net/2020/10/19/seaborn%e7%9a%846%e4%b8%aa%e7%ae%80%e5%8d%95%e6%8a%80%e5%b7%a7/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Seaborn的6个简单技巧

磐石 • 2020年10月19日 pm9:44 • 机器学习

作者|Zolzaya Luvsandorj
编译|VK
来源|Towards Datas Science

在这篇文章中,我们将探讨一些简单的方法来定制你的图表,使它们在美学上更好。我希望这些简单的技巧能帮助你得到更好看的图。

本文中的脚本在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

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

# 图
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm', 
                alpha=0.7, hue='species', size='gender')

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


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

技巧1:分号

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

# 图
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm', 
                alpha=0.7, hue='species', size='gender');

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

技巧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');

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

# 图
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));

如果你想知道如何知道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));

这里还有一些其他的选择可以尝试:“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));

它更容易辨认,不是吗?另一个可以尝试的选项是:“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));

如果找不到你喜欢的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));

如果颜色名称不能很好地捕捉到你所追求的,你可以使用十六进制颜色构建自己的调色板来访问更广泛的选项(超过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));

技巧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')

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


我希望你学会了一些简单的方法来调整你的图表,这不用花太多时间。我希望这篇文章能给你一些初步的想法,让你开始个性化你的图表,并使它们更具视觉上的美。如果你感兴趣,以下是我的一些帖子的链接:

原文链接:https://towardsdatascience.com/6-simple-tips-for-prettier-and-customised-plots-in-seaborn-python-22f02ecc2393

欢迎关注磐创AI博客站:
http://panchuang.net/

sklearn机器学习中文官方文档:
http://sklearn123.com/

欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/

原创文章,作者:磐石,如若转载,请注明出处:https://panchuang.net/2020/10/19/seaborn%e7%9a%846%e4%b8%aa%e7%ae%80%e5%8d%95%e6%8a%80%e5%b7%a7/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK