Seaborn library is a higher-level encapsulation of
Matplotlib library, which is equivalent to providing various statistical chart templates. We just need to pass our data into the corresponding methods. Therefore, if you understand the basic drawing methods in
Matplotlib, it will be very easy to learn this topic List representation.)
First of all, let's take a look at the main learning contents and space arrangement of Seaborn library.
Seaborn overall layout and style setting
Seaborn palette and color setting
Seaborn single variable analysis drawing (histogram, bar chart)
Seaborn regression analysis plot
Seaborn draws scatter plot (scatter plot, cluster scatter plot)
Seaborn draws box diagram and violin diagram
Seaborn drawing heat map
Multi graph drawing of Seaborn and usage of facetgrid
For the content of the Seaborn library, the split is relatively detailed. The purpose of this is to make each article targeted, but not to read each article for too long. I hope you can understand it. OK, let's officially enter the study.
seaborn.set () set style
First of all, take a look seaborn.set () function parameter: seaborn.set (context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_ scale=1, color_ Codes = true, RC = none). From this set() function, we can set the background color, style, font, etc. Let's take an example. First, import the dependent packages. Note that the "ා% Matplotlib inline is a magic function built into jupyter notebook, which can be omitted with% Matplotlib inline plt.show ().
import seaborn as sns
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
Then, we define a function that generates 100 variables from 0 to 15, and then uses this variable to draw six curves.
def sinplot(flip=2):
x = np.linspace (0, 15, 100)
for i in range(1, 6):
plt.plot (x, np.sin (x + i * .5) * (7 - i) * flip)
#Call this function
sinplot
No style set
In the figure above, we do not set any style. Next, we call sns.set () function to change the style to see what effect.
sns.set (style='white')
sinplot()
Set the style = white effect through the set() function
Then, the problem comes. Some people will say that if you change the value of any one of the parameters of the set() function, the drawing effect will change. How do we know which combination is the best? Do we have to test them one by one? Of course not. Seaborn provides 5 default styles. We only need to choose a favorite style in the actual drawing. Let's take a look at the usage and effect of these 5 styles.
seaborn.set_ Style () uses five default styles
Function parameters: seaborn.set_ Style (style = none, RC = none), here the optional parameter values of style are: darkgrid, whitegrid, dark, white, ticks. Let's see the effect of each style by setting different styles.
sns.set (style='white')
sns.set (style='whitegrid')
sns.set (style='darkgrid')
sns.set (style='dark')
sns.set (style='ticks')
sinplot()
style=whitegrid
style=darkgrid
style=ticks
Of course, in addition to using the built-in five styles, we can also do some personalization through the following functions.
seaborn.despine ()
This function can remove the upper and right axes of the image. Let's see the effect.
sinplot()
sns.despine ()
Despine() removes the axis
The upper and right axes are removed by default. Of course, we can also remove other axes. Just change the parameter value representing four sides to true. Here is the parameter of this function seaborn.despine (Fig = none, ax = none, top = true, right = true, left = false, bottom = false, offset = none, trim = false).
sns.set (style='ticks')
sinplot()
sns.despine (offset=50)
offset=50
Open a style with
As we have learned in Matplotlib, we can add multiple subgraphs in a figure object. What should we do if different subgraphs use different styles? Very simple, use with to open a certain style, and then use with to open the grid for all the graphs drawn under with. Let's take a look at the code.
with sns.axes_ style("darkgrid"):
plt.subplot (211)
sinplot(4)
plt.subplot (212)
sinplot(-4)
Use with to make each subgraph use a different style
seaborn.set_ context()
seaborn.set_ context(context=None, font_ Scale = 1, RC = none) this function is also used to set the drawing background parameters. It mainly affects the effect of labels, lines and other elements, but does not affect the overall style. It is a little different from style. This function uses notebook by default. Other context optional values are: paper, talk, post. Let's take a look at the specific effects.
sns.set_ context("paper")
plt.figure (figsize=(8, 6))
sinplot()
context=paper
sns.set_ context("talk")
plt.figure (figsize=(8, 6))
sinplot()
context=talk
sns.set_ context("poster")
plt.figure (figsize=(8, 6))
sinplot()
context=poster
sns.set_ context("notebook", font_ scale=1.5, rc={" lines.linewidth ": 2.5})
sinplot()
context=notebook