Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Seaborn to draw Common Chart in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Python how to use Seaborn to draw common charts, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Seaborn is a data visualization library built on matplotlib, which is closely integrated with the pandas data structure in Python. Visualization is a core part of Seaborn and can help you explore and understand data.

To understand Seaborn, you must be familiar with Numpy and Matplotlib, as well as pandas.

Seaborn provides the following features:

API-oriented dataset to determine the relationship between variables.

Automatic calculation and drawing of linear regression curve.

It supports high-level abstract rendering of multiple images.

Visualize univariate and bivariate distributions.

These are just some of the features provided by Seaborn, and there are many other features that we can explore here.

To introduce the Seaborn library, the command is:

Import seaborn as sns

Using Seaborn, we can draw all kinds of graphics, such as:

Distribution curve

Pie chart and bar chart

Scatter plot

Pairing diagram

Thermal map

In this article, we use the Google Playstore dataset downloaded from Kaggle.

1. Distribution curve

We can compare the distribution of Seaborn with the histogram of Matplotlib. They all provide very similar functions. What we draw here is not the frequency diagram in the histogram, but the approximate probability density on the y-axis.

We will use sns.distplot () in the code to draw the distribution map.

Before we go any further, let's access our dataset.

Import pandas as pd

Import numpy as np

Pstore = pd.read_csv ("googleplaystore.csv")

Pstore.head (10) accesses datasets from our system

The dataset is like this.

Google playback Store dataset obtained from Kaggle

Now, let's see what happens if we map the distribution of the "Rating" column from the above dataset.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Create a distribution plot for rating

Sns.distplot (pstore.Rating)

Plt.show () Rating column distribution map code

The distribution diagram of the Rating column looks like this

Here, the curve (KDE) shows an approximate probability density curve on the distribution map.

Similar to the histogram in matplotlib, in terms of distribution, we can also change the number of categories to make the diagram easier to understand.

We just need to add the number of categories to the code

# Change the number of bins

Sns.distplot (inp1.Rating, bins=20, kde = False)

Plt.show ()

The image looks like this.

Distribution map of the number of specific categories

In the figure above, there is no probability density curve. To remove the curve, we simply write 'kde = False' in the code.

We can also provide the title and color of containers similar to matplotlib to the distribution map. Let's look at its code.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Create a distribution plot for rating

Sns.distplot (pstore.Rating, bins=20, color= "g")

Plt.title ("Distribution of app ratings", fontsize=20, color = 'red')

Plt.show ()

The distribution of the same column of rating is as follows:

A titled distribution map

Stylize Seaborn graphics

One of the biggest advantages of using Seaborn is that it provides a wide range of default style options for graphics.

These are the default styles provided 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 only need to write a single line of code to incorporate these styles into our diagram.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Adding dark background to the graph

Plt.style.use ("dark_background")

# Create a distribution plot for rating

Sns.distplot (pstore.Rating, bins=20, color= "g")

Plt.title ("Distribution of app ratings", fontsize=20, color = 'red')

Plt.show ()

After applying a dark background to our chart, the distribution looks like this

Distribution of dark background 2. Pie chart and bar chart

Pie charts are often used to analyze how digital variables change from category to category.

In the dataset we use, we will analyze the implementation of the first four categories in the content Rating column.

First, we will do some data cleaning / mining on the content Rating column and check the categories in it.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Analyzing the Content Rating column

Pstore ['Content Rating'] .value_counts ()

The list of categories is

Number of Rating columns

According to the above output, since the number of "adults over 18" and "unrated" is much lower than others, we will remove these categories from the content rating and update the dataset.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Remove the rows with values which are less represented

Pstore = pstore [~ pstore ['Content Rating'] .isin (["Adults only 18 +", "Unrated"])]

# Resetting the index

Pstore.reset_index (inplace=True, drop=True)

# Analyzing the Content Rating column again

Pstore ['Content Rating'] .value_counts ()

The categories that appear in the "Rating" column after the update are:

Rating count after updating the dataset

Now let's draw a pie chart for the categories that appear in the Rating column.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Plotting a pie chart

Plt.figure (figsize= [9. 7])

Pstore ['Content Rating']. Value_counts (). Plot.pie ()

Plt.show ()

The pie chart of the above code is as follows

Pie chart for Rating

From the pie chart above, we cannot correctly infer "all 10 +" and "mature 17 +". When the values of these two types of people are somewhat similar, it is difficult to assess the differences between them.

We can overcome this situation by drawing the above data as a bar chart.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Plotting a bar chart

Plt.figure (figsize= [9. 7])

Pstore ['Content Rating']. Value_counts (). Plot.barh ()

Plt.show ()

The bar chart is as follows

Bar chart of the Rating column

Similar to pie charts, we can also customize bar charts, using different bar chart colors, chart titles, and so on.

3. Scatter plot

So far, we have only dealt with one numeric column in the dataset, such as rating, comment, or size. But what if we had to infer the relationship between two numeric columns, such as "rating and size" or "rating and comment"?

We can use a scatter chart when we want to draw the relationship between any two numeric columns in the dataset. This diagram is the most powerful visualization tool in the field of machine learning.

Let's take a look at what the scatter chart of the two numeric columns in the dataset rating and size looks like. First, we will draw a graph using matplotlib, and then we will see what it looks like in seaborn.

Scatter plot using matplotlib

# import all the necessary libraries

# Plotting the scatter plot

Plt.scatter (pstore.Size, pstore.Rating)

Plt.show ()

The picture looks like this.

Scatter plot using Matplotlib

Scatter plot using Seaborn

In the histogram and scatter code, we will use sn. Joinplot ().

The code for the sns.scatterplot () scatter chart.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Plotting the same thing now using a jointplot

Sns.jointplot (pstore.Size, pstore.Rating)

Plt.show ()

The scatter plot of the above code is as follows

Scatter plot using Seaborn

The main advantage of using a scatter chart in seaborn is that we will get both a scatter chart and a histogram.

If we want to see only scatter plots instead of combinatorial diagrams in our code, just change it to "scatterplot"

Regression curve

The regression graph establishes the regression line between the two numerical parameters in the joint graph (scatter graph), and helps to visualize their linear relationship.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Plotting the same thing now using a jointplot

Sns.jointplot (pstore.Size, pstore.Rating, kind = "reg")

Plt.show ()

The picture looks like this.

Using jointplot for regression Analysis in Seaborn

We can infer from the above chart that when the price of app rises, the rating will rise steadily.

4. Pairing diagram

We can use a pairing diagram when we want to see the relationship pattern between more than three different numeric variables. For example, suppose we want to understand how a company's sales are affected by three different factors, in which case a pair diagram would be very useful.

Let's create a pair of graphs for the comment, size, price, and rating columns of the dataset.

We will use sns.pairplot () in our code to draw multiple scatter plots at a time.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Plotting the same thing now using a jointplot

Sns.pairplot (pstore [['Reviews',' Size', 'Price','Rating']])

Plt.show ()

The output graph of the above graph looks like this

Pairing diagrams using Seaborn

For non-diagonal views, the image is a scatter plot between two numeric variables

For diagonal views, it draws a bar chart because the two axes (x _ moment y) are the same.

5. Thermal map

The heat map represents the data in two dimensions. The ultimate goal of a heat map is to display a summary of the information in a color chart. It uses the concept of color intensity to visualize a series of values.

We often see the following types of graphics in football matches

Heat map of football players

Create this type of graph in Seaborn.

We will draw a visualization using sn .heatmap ().

When you have the following data, we can create a heat map.

The above table is created using a PivotTable from Pandas.

Now, let's see how to create a heat map for the above table.

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# # Plot a heat map

Sns.heatmap (heat)

Plt.show ()

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

The heat map is as follows

Create a default heat map using Seaborn

We can customize the picture above, or we can change the color gradient to darken the maximum value and lighten the minimum value.

The updated code looks like this

# importing all the libraries

Import numpy as np

Import pandas as pd

Import matplotlib.pyplot as plt

Import seaborn as sns

# Applying some customization to the heat map

Sns.heatmap (heat, cmap = "Greens", annot=True)

Plt.show ()

The heat map of the above code looks like this

After reading the above with some custom heat map code, have you mastered how to use Seaborn to draw common charts in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report