Seaborn Distplot:综合指南

在本文中,我们将专注于 Seaborn Distplot的细节。


什么是Seaborn Distplot?

一个 Distplot或分布图,描述了数据分布的变化,Seaborn Distplot代表了连续数据变量的整体分布。

Seaborn 模块与 Matplotlib 模块一起用来描绘其中的不同变异,Distplot 通过 histogram 和与其结合的线来描绘数据。


创建一个海洋分布式

Python Seaborn 模块包含各种函数来绘制数据并描绘数据变异. 使用 `seaborn.distplot() 函数来绘制 distplot. Distplot 代表数据的单变分布,即变量的数据分布与密度分布。

合成:**

1seaborn.distplot()

seaborn.distplot() 函数接受数据变量作为参数,并以密度分布返回序列。

例子一:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5data = np.random.randn(200)
6res = sn.distplot(data)
7plt.show()

我们使用numpy.random.randn() 函数来生成随机数据值。

出发点:**

Creating A DistPlot

例子二:

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4import pandas as pd
5
6data_set = pd.read_csv("C:/mtcars.csv")
7data = pd.DataFrame(data_set['mpg'])
8res = sn.distplot(data)
9plt.show()

pandas.read_csv()函数将数据集加载到 Python 环境中。

出发点:**

Creating A DistPlot Using A Dataset


将标签添加到DistPlot的轴

Seaborn Distplot 可以通过使用以下语法将数据值转换为 Pandas系列来提供轴的标签:

合成:**

1pandas.Series(data,name='name')
2seaborn.distplot()

Pandas 系列包含一个参数名称,用于设置数据轴的标签。

** 例子:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5data = np.random.randn(200)
6res = pd.Series(data,name="Range")
7plot = sn.distplot(res)
8plt.show()

出发点:**

Creating A DistPlot Using Series


Seaborn DistPlot 与 Kernel Density Estimate Plot 相结合

Seaborn Distplot 还可以与 Kernel Density Estimate Plot一起分类,以估计连续变量在各种数据值中分布的概率。

合成:**

1seaborn.distplot(data,kde=True)

kde参数被设置为True,以允许Kernel Density Plot和distplot。

** 例子:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5data = np.random.randn(100)
6res = pd.Series(data,name="Range")
7plot = sn.distplot(res,kde=True)
8plt.show()

出发点:**

DistPlot With KDE


使用Seaborn DistPlot和Rug Plot可视化数据

我们可以将Seaborn Distplot与Rug Plot绘制,以描绘与单变数据变量有关的数据对比分布。

合成:**

1seaborn.distplot(data, rug=True, hist=False)

背部参数必须设置为真实,以便允许地毯的分布。

** 例子:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5data = np.random.randn(100)
6res = pd.Series(data,name="Range")
7plot = sn.distplot(res,rug=True,hist=False)
8plt.show()

出发点:**

DistPlot With Rug Plot


沿垂直轴的Seaborn Distplot

整个Distplot可以用下面的语法绘制到 **y轴上:

合成:**

1seaborn.distplot(data,vertical=True)

必须将垂直参数设置为真实,以便在y轴上绘制分布图。

** 例子:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5data = np.random.randn(100)
6
7plot = sn.distplot(data,vertical=True)
8
9plt.show()

出发点:**

DistPlot With Vertical Axis


使用 seaborn.set() 函数设置不同的风格

Seaborn 具有多种内置功能,可为插图添加额外的背景功能. 使用 `seaborn.set() 函数来为分布插图设置不同的背景。

合成:**

1seaborn.set(style)

** 例子**:

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4sn.set(style='dark',)
5data = np.random.randn(500)
6
7plot = sn.distplot(data)
8
9plt.show()

出发点:**

DistPlot With Different Background


设置自定义颜色到 Seaborn DistPlot

我们可以将不同的颜色设置到 distplot 来添加到数据的可视化中,使用 seaborn.distplot() 函数的``颜色`参数。

合成:**

1seaborn.distplot(data, color='color')

** 例子:**

1import numpy as np
2import seaborn as sn
3import matplotlib.pyplot as plt
4
5sn.set(style='dark',)
6data = np.random.randn(500)
7plot = sn.distplot(data,color='purple')
8
9plt.show()

出发点:**

DistPlot With Different Color


结论

因此,Seaborn 模块和 Matplotlib 模块有助于数据可视化并描述数据的分布。

我强烈建议所有读者阅读Python Matplotlib模块(/社区/教程/python-matplotlib#python-matplotlib),以了解数据可视化的基础知识。


参考

Published At
Categories with 技术
comments powered by Disqus