seaborn
Hey, folks! In our Seaborn tutorial, we will be focusing on Seaborn Kdeplot.
嘿伙计! 在我们的Seaborn教程中,我们将专注于Seaborn Kdeplot 。
Kdeplot
is a Kernel Distribution Estimation Plot which depicts the probability density function of the continuous or non-parametric data variables i.e. we can plot for the univariate or multiple variables altogether. Using the Python Seaborn module, we can build the Kdeplot with various functionality added to it.
Kdeplot
是一个内核分布估计图,它描述了连续或非参数数据变量的概率密度函数,即,我们可以为单变量或多个变量一起绘制图。 使用Python Seaborn模块 ,我们可以构建带有各种功能的Kdeplot。
In order to use the Seaborn module, we need to install and import the module using the below command:
为了使用Seaborn模块,我们需要使用以下命令安装和导入该模块:
pip install seaborn
import seaborn
The seaborn.kdeplot() function is used to plot the data against a single/univariate variable. It represents the probability distribution of the data values as the area under the plotted curve.
seaborn.kdeplot()函数用于根据单个/单变量变量绘制数据。 它将数据值的概率分布表示为绘制曲线下的面积。
Syntax:
句法:
seaborn.kdeplot(data)
Example 1:
范例1:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data)
plt.show()
In the above example, we have generated some random data values using the numpy.random.randn() function.
在上面的示例中,我们使用numpy.random.randn()函数生成了一些随机数据值。
Output:
输出:
Example 2:
范例2:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data,color='green',shade=True)
plt.show()
In the above example, we have highlighted the plot using the parameter – ‘shade
‘ to highlight the area under the curve. Further, we can set different colors to the plot using the parameter – ‘color
‘.
在上面的示例中,我们使用参数-“ shade
”突出显示了曲线,以突出显示曲线下的区域 。 此外,我们可以使用参数“ color
”为绘图设置不同的颜色。
Output:
输出:
Seaborn Kdeplots can even be used to plot the data against multiple data variables or bivariate(2) variables to depict the probability distribution of one with respect to the other values.
Seaborn Kdeplots甚至可以用于针对多个数据变量或bivariate(2)变量绘制数据,以描绘一个变量相对于其他值的概率分布。
Syntax:
句法:
seaborn.kdeplot(x,y)
Thus, the distribution is represented as a contour plot depicting the relationship of the distribution between the two data variables.
因此,该分布表示为等高线图,描绘了两个数据变量之间的分布关系。
Example:
例:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],color='blue',shade=True)
plt.show()
Output:
输出:
We can plot the Kdeplots along the y-axis using the below syntax:
我们可以使用以下语法沿y轴绘制Kdeplots:
Syntax:
句法:
seaborn.kdeplot(data,vertical=True)
Thus, by setting the ‘vertical
‘ parameter to True, we can plot the distribution against the y-axis.
因此,通过将“ vertical
”参数设置为True ,我们可以针对y轴绘制分布。
Example:
例:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],vertical=True,color='blue',shade=True)
plt.show()
Output:
输出:
Different color palettes can be used along with the Seaborn plots to visualize the data in a better manner using the ‘cmap
‘ parameter.
可以将不同的调色板与Seaborn图一起使用,以使用' cmap
'参数以更好的方式可视化数据。
Different types of color palettes are available at Matplotlib Colormap.
Matplotlib Colormap提供了不同类型的调色板。
Syntax:
句法:
seaborn.kdeplot(data,cmap)
Example:
例:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],shade=True,cmap="Purples_d")
plt.show()
Output:
输出:
The two shaded Bivariate Kdeplots help in understanding the variation of the data in terms of the probability distribution of the bivariate group of data variables.
两个阴影阴影的双变量Kdeplots有助于根据数据变量的双变量组的概率分布来理解数据的变化。
Example:
例:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d")
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Blues")
plt.show()
Output:
输出:
A colorbar
maps the pictorial representation of values against the original data values and helps visualize the data in a better manner.
colorbar
将值的图形表示形式映射到原始数据值,并以更好的方式帮助可视化数据。
Syntax:
句法:
seaborn.kdeplot(data,cbar=True)
Example:
例:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res=sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d",cbar=True)
plt.show()
Output:
输出 :
Seaborn module is purely built upon the Matplotlib module and the combination is extensively used to visualize the data in different forms.
Seaborn模块纯粹基于Matplotlib模块构建,并且该组合被广泛用于以不同形式可视化数据。
I would strongly recommend the readers to go through Python Matplotlib Tutorial for a better understanding about the basics of data visualization.
我强烈建议读者阅读Python Matplotlib教程 ,以更好地了解数据可视化的基础。
翻译自: https://www.journaldev.com/40204/seaborn-kdeplot
seaborn