1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > seaborn箱线图_Seaborn线图的数据可视化

seaborn箱线图_Seaborn线图的数据可视化

时间:2021-07-14 22:16:28

相关推荐

seaborn箱线图_Seaborn线图的数据可视化

seaborn箱线图

Hello, folks! In this article, we will be taking the Seaborn tutorial ahead and understanding theSeaborn Line Plot. We recently covered Seaborn HeatMaps so feel free to have a look if you’re interested in learning more about heatmaps.

大家好! 在本文中,我们将继续进行Seaborn教程,并了解Seaborn Line Plot。 我们最近介绍了Seaborn HeatMaps,因此如果您有兴趣了解有关热图的更多信息,请随时查看。

什么是线图? (What is a Line Plot?)

Seaborn as a library is used inData visualizationsfrom the models built over the dataset to predict the outcome and analyse the variations in the data.

Seaborn是一个库,可用于在数据集中建立的模型的数据可视化中,以预测结果并分析数据的变化。

Seaborn Line Plotsdepict the relationship between continuous as well as categorical values in a continuous data point format.

Seaborn线图以连续数据点格式描述连续值和分类值之间的关系。

Throughout this article, we will be making the use of the below dataset to manipulate the data and to form the Line Plot. Please go through the below snapshot of the dataset before moving ahead.

在整个本文中,我们将使用以下数据集来操纵数据并形成线图。 在继续操作之前,请仔细阅读下面的数据集快照。

In the below dataset, the data variables — ‘cyl‘, ‘vs‘, ‘am‘, ‘gear‘ and ‘carb‘ arecategorical variablesbecause all the data values fall under a certain category or range of values.

在下面的数据集中,数据变量“cyl”,“vs”,“am”,“gear”和“carb”是分类变量,因为所有数据值都属于某个类别或值范围。

While the remaining data column falls under the integer/continuous variablesbecause they carry discrete integer values with them.

而剩余的数据列属于整数/连续变量,因为它们带有离散整数值。

Input Dataset:

输入数据集:

MTCARS DatasetMTCARS数据集

绘制您的第一个Seaborn线图 (Plotting Your First Seaborn Line Plot)

In order to start with Line Plots, we need to install and import theSeaborn Libraryinto the Python environment by using the below command:

为了从线图开始,我们需要使用以下命令将Seaborn库安装并导入到Python环境中:

Syntax:

句法:

pip install seaborn

Once you are done with the installation, import the library to the current working environment and use the functions

完成安装后,将库导入到当前工作环境并使用功能

Syntax:

句法:

import seaborn

For the entire series of Seaborn, we will be usingMatplotlib libraryto plot the data and show it in a proper visualized manner.

对于整个Seaborn系列,我们将使用Matplotlib库绘制数据并以适当的可视化方式显示。

使用Seaborn创建单线图 (Creating Single LinePlot with Seaborn)

We can supply discrete values or use data sets to create a Seaborn line plot.

我们可以提供离散值或使用数据集来创建Seaborn线图。

Syntax:

句法:

seaborn.lineplot(x, y, data)

x: Data variable for the x-axisx:x轴的数据变量y: The data variable for the y-axisy:y轴的数据变量data: The object pointing to the entire data set or data valuesdata:指向整个数据集或数据值的对象

Example 1:Using random data to create a Seaborn Line Plot

示例1:使用随机数据创建Seaborn线图

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltYear = [, , , , , , ]Profit = [80, 75.8, 74, 65, 99.5, 19, 33.6]data_plot = pd.DataFrame({"Year":Year, "Profit":Profit})sns.lineplot(x = "Year", y = "Profit", data=data_plot)plt.show()

In the below line-plot, we can witness the linear relationship between the two data variables — ‘Year’ and ‘Profit’.

在下面的线图中,我们可以看到两个数据变量“年”和“利润”之间的线性关系。

Output:

输出:

LinePlot Example 1LinePlot示例1

Example 2:Using a Dataset to create a Line Plot and depict the relationship between the data columns.

示例2:使用数据集创建线图并描述数据列之间的关系。

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,:5]sns.lineplot(x = "drat", y = "mpg",data=info)sns.set(style='dark',)plt.show()

Input Dataset:

输入数据集:

Input Dataset Seaborn LinePlot输入数据集Seaborn LinePlot

Output:

输出:

LinePlot Example 2LinePlot示例2

多个Seaborn线图 (Multiple Seaborn Line Plots )

We can create multiple lines to visualize the data within the same space or plots. We can use the same or multiple data columns/data variables and depict the relationship between them altogether.

我们可以创建多条线以可视化同一空间或图中的数据。 我们可以使用相同或多个数据列/数据变量,并完全描述它们之间的关系。

1.使用hue参数为多个数据点创建颜色 (1. Using the hue Parameter To Create Color Hue for Multiple Data Points)

The parameterhuecan be used to group the different variables of the dataset and would help depict the relationship between the x and the y-axis data columns with the column passed as a value to the parameter.

参数hue可用于对数据集的不同变量进行分组,并有助于描述x和y轴数据列之间的关系,并将该列作为值传递给参数。

Syntax:

句法:

seaborn.lineplot(x,y,data,hue)

Example:

例:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,:5]sns.lineplot(x = "drat", y = "mpg", data=info, hue="cyl")plt.show()

As seen in the below plot, it represents three lines with a different color scheme to depict the relationship between the ‘drat‘, ‘mpg‘ and ‘cyl‘ respectively.

如下图所示,它代表了三行具有不同配色方案的线,分别描绘了'drat','mpg'和'cyl'之间的关系。

Output:

输出:

Multiple Seaborn LinePlot多个Seaborn线图

2.使用style参数绘制不同类型的线 (2. Using the style Parameter to Plot Different Types of Lines)

We can set the style parameter to a value that we’d like to display along with the x and the y-axis and also specify different line structures: dash, dots(markers), etc.

我们可以将样式参数设置为一个要与x和y轴一起显示的值,还可以指定不同的线结构:破折号,点(标记)等。

Syntax:

句法:

seaborn.lineplot(x, y, data, style)

Example 2:

范例2:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,:5]sns.lineplot(x = "drat", y = "mpg", data=info, hue="cyl", style="cyl")plt.show()

As seen clearly, the plot represents the ‘cyl’ values in relation with ‘mpg’ and ‘drat’ with different line structures i.e. plain line, dashes and markes.

可以清楚地看到,该图表示与“ mpg”和“ drat”相关的“ cyl”值,并具有不同的线结构,即普通线,虚线和标记。

Output:

输出:

Line Plot With style Parameter带有样式参数的线图

3.使用size参数在Seaborn中绘制多个线图 (3. Using size parameter to plot multiple line plots in Seaborn)

We can even use thesizeparameter ofseaborn.lineplot() functionto represent the multi data variable relationships with a varying size of line to be plotted. So it acts as a grouping variable with different size/width according to the magnitude of the data.

我们甚至可以使用seaborn.lineplot() functionsize参数来表示具有可变大小的待绘制线的多数据变量关系。 因此,它根据数据的大小充当大小/宽度不同的分组变量。

Syntax:

句法:

seaborn.lineplot(x, y, data, size)

Example 3:

范例3:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,]sns.lineplot(x = "drat", y = "mpg", data=info, hue="gear",style="gear",size="gear")plt.show()

Input Dataset:

输入数据集:

Dataset For Multiple Line Plot多线图数据集

Output:

输出:

Line Plot With size Parameter带有大小参数的线图

与线图一起使用不同的调色板 (Using different color palette along with Line Plot)

Seaborn colormap and palette define the color range for the visualization models. The parameterpalettealong withhuecan be used for determining the color encoding scheme in terms of the data variable.

Seaborn颜色图和调色板定义了可视化模型的颜色范围。 参数palettehue可用于根据数据变量确定颜色编码方案。

For more color palettes, you can reference the link here: Color Palette

有关更多调色板,您可以在此处引用链接: 调色板

Syntax:

句法:

seaborn.lineplot(x,y,data,hue,palette)

Example:

例:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,]sns.lineplot(x = "drat", y = "mpg", data=info, hue="gear", palette = "Set1")plt.show()

Output:

输出:

Line Plot Palette
线图调色板

误差线添加到线图 (Addition of Error Bars to Line Plot)

Line Plots can be used to define the confidence levels/intervals in the plots to depict the error rates through the use oferr_styleparameter.

线图可用于定义图中的置信度水平/区间,以通过使用err_style参数来描述错误率。

Syntax:

句法:

seaborn.lineplot(x,y,data,err_style="bars")

Example:

例:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,]sns.lineplot(x = "cyl", y = "mpg",data=info, err_style="bars")plt.show()

Output:

输出:

Line Plot With err_style Parameter带有err_style参数的线图

使用seaborn.set()函数设置不同的样式 (Setting different style using seaborn.set() function)

Pythonseaborn.set() functioncan be used to display the plot in a different background style.

Pythonseaborn.set() function可用于以不同背景样式显示图。

Syntax:

句法:

seaborn.set(style)

Example:

例:

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = pd.read_csv("C:/mtcars.csv")info = data.iloc[1:20,]sns.lineplot(x = "cyl", y = "mpg",data=info,hue="gear")sns.set(style='dark',)plt.show()

Output:

输出:

Line Plot With set() function带有set()函数的线图

结论 (Conclusion)

Thus, in this article, we have understood the Line Plots and the variations associated with it.

因此,在本文中,我们已经了解了线图及其相关的变化。

I strongly recommend the readers to go through Python Matplotlib tutorial to understand the Line Plots in a better manner.

我强烈建议读者阅读Python Matplotlib教程 ,以更好的方式了解线图。

参考资料 (References)

Seaborn Line Plot — Official DocumentationSeaborn线图—官方文档

翻译自: /39342/seaborn-line-plot

seaborn箱线图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。