Seaborn data visualization (1)

Table of contents

1. Introduction to seaborn

 2. Seaborn drawing style settings

21. Parameter description:

2.2 Example:


1. Introduction to seaborn

Seaborn is a Python library for data visualization, which is a high-level plotting library built on top of Matplotlib. The goal of Seaborn is to make the task of plotting easy while producing beautiful and informative graphics.

Seaborn provides a set of advanced functions and theme styles for statistical data visualization, making it easy and fast to draw various types of statistical graphics. The statistical graphics it supports include common line charts, histograms, scatter plots, box plots, kernel density plots, etc. In addition, Seaborn also provides special support for categorical data, making it easy to draw graphs such as grouped bar charts and violin plots.

Seaborn also has built-in theme styles that make it easy to tweak the look of your graphs to make them more professional and consistent. In addition, Seaborn also provides support for functions such as logarithmic axis, color palette, and handling missing data, which further enhances the flexibility and expressiveness of visualization.

To use Seaborn, you need to first install it and import the corresponding library. You can install Seaborn in a Python environment using the pip command: ` pip install seaborn` . Once installed, you can use ` import seaborn as sns` to import the Seaborn library and start using its functions and methods to draw statistical graphs.

There are 5 categories and 21 kinds of drawings in seaborn, as follows:

 2. Seaborn drawing style settings

Seaborn implements style setting through the set function. The format of the set function is as follows:

seaborn.set(style=None, palette=None, font='sans-serif', font_scale=1, color_codes=False, rc=None)

21. Parameter description:

  • style: Specify the overall style of the graph, which can be a predefined style name, such as: "whitegrid", "darkgrid", "ticks", or a custom dictionary. The default value is None, which means to use the default style.
  • palette: Specifies the color palette, which can be a predefined palette name, such as: "deep", "muted", "bright", etc., or a custom color list or color space object. The default value is None, which means to use the default palette.
  • font: Specifies the font family name or font list. Defaults to 'sans-serif', a sans-serif font.
  • font_scale: Specifies the scaling factor of the font. The default is 1, which means use the default font size.
  • color_codes: Whether to use color codes (eg: '#FF0000' ) instead of named colors. Defaults to False, which means use named colors.
  • rc: A parameter of dictionary type, used to override the settings of other parameters.

It should be noted that the seaborn.set function is usually called at the very beginning after importing the seaborn library to set the global drawing parameters and ensure the consistency of the styles and parameters used throughout the drawing process. Global settings can also be overridden via parameters in specific plotting functions.

2.2 Example:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
def sinplot(flip = 2):
    x = np.linspace(0,20, 50)
    for i in range(1,5):
        plt.plot(x, np.cos(x + i * 0.8) * (9 - 2*i) * flip)
sinplot()

Result graph:

Before using seabron.set to set the style, the running result is the drawing style under the default parameters of Matplotlib, and then use seabron.set to set the style.

sns.set(style = 'darkgrid',font_scale = 1.5)
sinplot()

Result graph:

 

If you need to convert to seaborn's default drawing style, just call the sns.set() method.

sns.set()
sinplot()

 Result graph:

 


Guess you like

Origin blog.csdn.net/m0_64087341/article/details/132384029