# Seaborn：Python

Seaborn is a data visualization library built on top of matplotlib and closely integrated with pandas data structures in Python. Visualization is the central part of Seaborn which helps in exploration and understanding of data.

Seaborn是建立在matplotlib之上的数据可视化库，并与Python中的pandas数据结构紧密集成。 可视化是Seaborn的核心部分，有助于探索和理解数据。

One has to be familiar with Numpy and Matplotlib and Pandas to learn about Seaborn.

Seaborn offers the following functionalities:

Seaborn提供以下功能：

1. Dataset oriented API to determine the relationship between variables.

面向数据集的API确定变量之间的关系。
2. Automatic estimation and plotting of linear regression plots.

自动估计和绘制线性回归图。
3. It supports high-level abstractions for multi-plot grids.

它支持多图网格的高级抽象。
4. Visualizing univariate and bivariate distribution.

可视化单变量和双变量分布。

These are only some of the functionalities offered by Seaborn, there are many more of them, and we can explore all of them here.

To initialize the Seaborn library, the command used is:

import seaborn as sns

Using Seaborn we can plot wide varieties of plots like:

1. Distribution Plots

分布图
2. Pie Chart & Bar Chart

饼图和条形图
3. Scatter Plots

散点图
4. Pair Plots

对图
5. Heat maps

热图

For this entirety of the article, we are using the dataset of Google Playstore downloaded from Kaggle.

## 1.分布图 (1. Distribution Plots)

We can compare the distribution plot in Seaborn to histograms in Matplotlib. They both offer pretty similar functionalities. Instead of frequency plots in the histogram, here we’ll plot an approximate probability density across the y-axis.

We will be using sns.distplot() in the code to plot distribution graphs.

Before going further, first, let’s access our dataset,

The dataset looks like this,

Now, let’s see how distribution plot looks like if we plot for ‘Rating’ column from the above dataset,

The Distribution Plot looks like this for Rating’s column,

“评分”列的“分布图”如下所示：

Here, the curve(KDE) that appears drawn over the distribution graph is the approximate probability density curve.

Similar to the histograms in the matplotlib, in distribution too, we can change the number of bins and make the graph more understandable.

We just have to add the number of bins in the code,

#Change the number of binssns.distplot(inp1.Rating, bins=20, kde = False)plt.show()

Now, the graph looks like this,

In the above graph, there is no probability density curve. To remove the curve, we just have to write ‘kde = False’ in the code.

We can also provide the title and color of the bins similar to matplotlib to the distribution plots. Let’s see the code for that,

The distribution graph, for the same column rating, looks like this:

Styling the Seaborn graphs

One of the biggest advantages of using Seaborn is, it offers a wide range of default styling options to our graphs.

These are the default styles offered by Seaborn.

'Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10'

We just have to write one line of code to incorporate these styles into our graph.

After applying the dark background to our graph, the distribution plot looks like this,

## 2.饼图和条形图 (2. Pie Chart & Bar Chart)

Pie Chart is generally used to analyze the data on how a numeric variable changes across different categories.

In the dataset we are using, we’ll analyze how the top 4 categories in the Content Rating column is performing.

First, we’ll do some data cleaning/mining to the Content rating column and check what are the categories in there.

Now, the categories list will be,

As per the above output, since the count of “Adults only 18+” and “Unrated” are significantly less compared to the others, we’ll drop those categories from the Content Rating and update the dataset.

The categories present in the “Content Rating” column after updating the sheet are,

Now, let’s plot Pie Chart for the categories present in the Content Rating column.

The Pie Chart for the above code looks like the following,

From the above Pie diagram, we cannot correctly infer whether “Everyone 10+” and “Mature 17+”. It is very difficult to assess the difference between those two categories when their values are somewhat similar to each other.

We can overcome this situation by plotting the above data in Bar chart.

Now, the bar Chart looks like the following,

Similar to Pie Chart, we can customize our Bar Graph too, with different Colors of Bars, the title of the chart, etc.

## 3.散点图 (3. Scatter Plots)

Up until now, we have been dealing with only a single numeric column from the dataset, like Rating, Reviews or Size, etc. But, what if we have to infer a relationship between two numeric columns, say “Rating and Size” or “Rating and Reviews”.

Scatter Plot is used when we want to plot the relationship between any two numeric columns from a dataset. These plots are the most powerful visualization tools that are being used in the field of machine learning.

Let’s see how the scatter plot looks like for two numeric columns in the dataset “Rating” & “Size”. First, we’ll plot the graph using matplotlib after that we’ll see how it looks like in seaborn.

Scatter Plot using matplotlib

#import all the necessary libraries#Plotting the scatter plotplt.scatter(pstore.Size, pstore.Rating)plt.show()

Now, the plot looks like this

Scatter Plot using Seaborn

We will be using sns.joinplot() in the code for scatter plot along with the histogram.

sns.scatterplot() in the code for only scatter plots.

The Scatter plot for the above code looks like,

The main advantage of using a scatter plot in seaborn is, we’ll get both the scatter plot and the histograms in the graph.

If we want to see only the scatter plot instead of “jointplot” in the code, just change it with “scatterplot

Regression Plot

Regression plots create a regression line between 2 numerical parameters in the jointplot(scatterplot) and help to visualize their linear relationships.

The graph looks like the following,

From the above graph, we can infer that there is a steady increase in the Rating if the Price of the apps increases.

## 4.配对图 (4. Pair Plots)

Pair Plots are used when we want to see the relationship pattern among more than 3 different numeric variables. For example, let’s say we want to see how a company’s sales are affected by three different factors, in that case, pair plots will be very helpful.

Let’s create a pair plot for Reviews, Size, Price, and Rating columns from of dataset.

We will be using sns.pairplot() in the code to plot multiple scatter plots at a time.

The output graph for the above graphs looks like this,

• For the non-diagonal views, the graph will be a scatter plot between 2 numeric variables

对于非对角线视图，图形将是2个数字变量之间的散点图

• For the diagonal views, it plots a histogram since both the axis(x,y) is the same.

对于对角线视图，由于两个轴(x，y)相同，因此它绘制了直方图

## 5.热图 (5. Heatmaps)

The heatmap represents the data in a 2-dimensional form. The ultimate goal of the heatmap is to show the summary of information in a colored graph. It utilizes the concept of using colors and color intensities to visualize a range of values.

Most of us would have seen the following type of graphics in a football match,

Heatmaps in Seaborn create exactly these types of graphs.

Seaborn中的热图正是创建了这些类型的图。

We’ll be using sns.heatmap() to plot the visualization.

When you have data as the following we can create a heatmap.

The above table is created using the Pivot table from Pandas. You can see how Pivot tables are created in my previous article Pandas.

Now, let’s see how we can create a heatmap for the above table.

In the above code, we have saved the data in the new variable “heat.”

The heatmap looks like the following,

We can apply some customization to the above graph, and also can change the color gradient so that the highest value will be darker in color and the lowest value will be lighter.

The updated code will be something like this,

The heatmap for the above-updated code looks like this,

If we observe, in the code we have given “annot = True”, what this means is, when annot is true, each cell in the graph displays its value. If we haven’t mention annot in our code, then the default value it takes is False.

Seaborn also supports some of the other types of graphs like Line Plots, Bar Graphs, Stacked bar charts, etc. But, they don’t offer anything different from the ones created through matplotlib.

Seaborn还支持其他一些类型的图形，例如折线图，条形图，堆积条形图等。但是，它们提供的功能与通过matplotlib创建的功能不同。

## 结论 (Conclusion)

So, this is how Seaborn works in Python and the different types of graphs we can create using seaborn. As I have already mentioned, Seaborn is built on top of the matplotlib library. So, if we are already familiar with the Matplotlib and its functions, we can easily build Seaborn graphs and can explore more depth concepts.

Thank you for reading and Happy Coding!!!

# 在这里查看我以前有关Python的文章 (Check out my previous articles about Python here)

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-15 796
12-23 10万+

10-26 652
05-09 1383
04-02 1万+
12-22 2127
08-21 448
12-16 8656
01-28 4801
10-13 3235
08-13 1130
08-20 5359
03-01 1万+
05-15 4万+
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。