Seaborn basic syntax and features

Introduction

Seaborn is a very popular visualization library in Python. Seaborn has a more advanced encapsulation based on Matplotlib, allowing users to draw scientific research paper graphics with rich statistical information using very little code. Seaborn is based on Matplotlib. The parameters of most plotting functions in Matplotlib can be used in Seaborn plotting functions. It has good support for other Python libraries (such as Numpy/Pandas/Scipy).

Install Matplotlib

python -m pip install matplotlib

Install Seaborn

pip install seaborn

Figure type

Plot types provided by Seaborn include

  • statistical relationships
  • Distributions of data
  • categorical data
  • Regression model analysis type (regressionModels)
  • Multi-plot grids

relationship diagram

The interrelationship and degree of interdependence between variables in a data set can be known through statistical analysis of correlations between variables .
The relational graph drawing function in Seaborn is as follows:

Data distribution diagram

Before analyzing or modeling data, we need to first understand the distribution of data, as well as basic information such as data coverage, central trend, and outliers.

Data distribution graph drawing function in Seaborn:

Categorical data type diagram

When faced with the situation of having discrete variables (categorical variables) in the data group, we can use the plotting function with the X-axis or Y-axis as the categorical axis to plot the categorical data type.

Common categorical data type graph drawing functions in Seaborn:


Regression model analysis diagram

We can use regression model analytical plots to represent the relationship between variables in a data set, and use statistical models to estimate the relationship between two sets of variables.

Regression analysis graph drawing function in Seaborn:

Multi-subgraph grid graph

Compared with Matplotlib, Seaborn provides multiple subgraph grid drawing functions, which can quickly realize the display of faceted graphs. When faced with drawing requirements such as drawing by data subsets, displaying subgraphs in rows or columns, and combinations of different types of graphs, the multi-subgraph grid drawing function can not only visually display the changes in each variable in the data set at one time, but also reduce Time to draw complex diagrams.

FacetGrid() function

The FacetGrid () function provided by Seaborn can realize the visual display of the distribution of any variable in the data set and the relationship between multiple variables in a subset of the data set .

FacetGrid() The function can realize numerical mapping in three dimensions: row, column, and hue. The row and column dimensions have an obvious correspondence with the resulting axis array. The hue variable can be regarded as the third dimension along the depth axis and is drawn with different colors . different levels of data.

core code;

import seaborn as sns
import matplotlib.pyplot as plt
g = sns.FacetGrid (df, col ='time ', hue = 'smoker ')
g.map (sns.regplot, "total_ bill", "tip")
g.add_legend ( )

Draw faceted plot results:

PairGrid() function

The PairGrid () function provided by Seaborn is mainly used to draw multi-subgraph grid-type graphs with pairwise relationships in the data set . In the PairGrid () function, each row and column is assigned a different variable, which results in a plot showing the relationship between pairs of variables in the data set. This kind of plot is also called a "scatter plot matrix".

Core code:

import Seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset ("penguins")
x_vars = ["body_mass_g", "bill_length_mm", "bill_depth_mm",]
y_vars = ["body_mass_g"]
g = sns.PairGrid(penguins,hue="species", x_vars=x_vars, y_vars=y_vars)
g.map_diag (sns.histplot, color=".3")
g.map_offdiag(sns.scatterplot)
g.add_legend ()

Drawing styles, color themes, and drawing element scaling

Compared with Matplotlib, Seaborn has more drawing styles and color themes. The color theme, drawing style and drawing element scaling can be set through the following functions.

sns.set_style("style name") #设置绘图风格
sns.set_palette("palette_name")#设置颜色主题
sns.set_context("context_name")#设置绘图元素缩放比例

The function provided by Seaborn set_theme ()includes all the functions of the above three functions, that is, by setting the parameters , and in set_theme()the function , you can control the color theme, drawing style and drawing element scaling respectively.palettestylecontext

drawing style

set_style()The optional values ​​of the parameter style include darkgrid, whitegrid, dark, whiteand ticks. The parameter rc is used to override the parameter mapping of the values ​​in the preset Seaborn style dictionary, and only updates some parameters in the style.
Here are the visualizations of the 4 drawing styles:

color theme

set_palette()The function includes three types of color themes: multi-color, single-color and two-color gradient. The display effects of different color themes can be sns.color_palette ()viewed through the function.

Visualization of some color theme options in Seaborn:

Drawing element scaling

set_context()contextThe optional values ​​of the parameters of the function are paper, notebook(default), talkand poster, and the scaling ratio increases in sequence.

Reference books: Ning Haitao. Guide to illustrating scientific research papers - based on Python[M]. Beijing: People's Posts and Telecommunications Press, 2023: 31-36.

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/132484798