Introduction of
Seaborn
Seaborn is a Python visualization library based on Matplotlib. It provides an advanced interface to draw attractive statistical graphs. Can make data visualization more convenient and beautiful. For Seaborn study, I recommend to go to the official website, where there are detailed tutorials and examples.
Seaborn commonly used function brief introduction
Direct use of Seaborn's landscaping function
Seaborn directly provides a variety of matplotlib drawing graphics beautification functions, can be used directly.
The sample
Draw the graph using Matplotlib,
Define a simple drawing function
Def sinplot (flip = 1) :
X = NP. linspace(0, 14, 100)
For I in range(1, 7):
Plt. plot(x, NP. Sin (x + I *.5) * (7-i) * flip)
Use the Matplotlib drawing effect directly
Draw with Seaborn's default theme embellished graphics
You can use Seaborn's set() function directly to beautify with its default theme just before drawing the graph
SNS. The set ()
Sinplot ()
Use Seaborn as the default theme rendering effect
Seaborn provides a variety of styled themes for use, which can be used directly via NSS.set_style ().
Provided for direct use of the topic:
Darkgrid, Whitegrid, dark, White, and ticks
The default is darkgrid
# Draw with theme provided by Seaborn
SNS. Set_style (" ticks ")
Sinplot ()
Draw the effect using the supplied theme 'Ticks'
We can use the nss.despine () function to remove the coordinate axis at the top and right of the image to make it look better
SNS. Set_style (' ticks')
Sinplot ()
SNS. Despine ()
Remove the top and right axis effects
Seaborn also provides a more friendly use of with, allowing you to use multiple drawing styles in a single image, with one style for all drawing in the WITH domain and another style for those not in the WITH domain.
# Use a style in with
With SNS. Axes_style (" darkgrid ") :
PLT, subplot (211).
Sinplot ()
Using another style that is not with, we can also use multiple with fields to use multiple styles
PLT, subplot (212).
Sinplot (1)
Use more than one style effect in one image
Of course, in addition to the themes provided by default, we can also define some drawing styles ourselves. You can define your own drawing topic by passing parameters to axes_style() and set_style().
Customize your drawing style by passing in the dictionary
SNS. Set_style (" darkgrid, "{" axes. Facecolor" : "9"})
Sinplot ()
We can see all the parameters available by calling the set_style() function directly.
The set_style() parameter is available
{' axes. Axisbelow: True,
'axes. Edgecolor' : '15',
'axes. Facecolor' : 'white',
'axes. The grid: False,
'axes. Labelcolor' : '15',
'axes. Our linewidth: 1.25,
'figure. Facecolor' : 'white',
'. The font family: [' sans-serif],
'the font. The sans-serif: [' Arial,
'the DejaVu Sans',
Sans' 'Liberation,
'the Bitstream Vera Sans',
'sans-serif].
'grid. Color:' 8 ',
'grid. Graphics.linestyle' : '-',
'image. Cmap' : 'the rocket',
'legend. Frameon: False,
'legend. Numpoints: 1,
'legend. Scatterpoints: 1,
'lines. Solid_capstyle' : 'round',
'text. Color:' 15 ',
'xtick. Color:' 15 ',
'xtick. Direction:' out ',
'xtick. Major. Size: 6.0,
'xtick. Minor. Size: 3.0.
'ytick. Color:' 15 ',
'ytick. Direction:' out ',
'ytick. Major. Size: 6.0,
'ytick. Minor. Size: 3.0}
We can also scale the elements in the image by modifying the context parameter.
Seaborn has built in multiple context styles: Paper, Notebook, talk, and poster
The default is to use notebook
SNS. Set_context (" poster ")
Sinplot ()
Set the Context effect to Poster
Use Seaborn's palette
Seaborn provides an easy-to-use palette for data visualization.
The sample
Qualitative (or categorical) palettes are best used to distinguish discrete blocks of data that do not have inherent ordering.
# Can be invoked to generate a palette using the color_palette()
Current_palette = SNS. Color_palette ()
SNS. Palplot (current_palette)
We can also customize a circular swatch as needed
# Use HLS color space and split into 8 colors
SNS. Palplot (SNS) color_palette (HLS, 8))
Seaborn also provides a method to extract matching colours to obtain two pairs of similar colours
SNS. Palplot (SNS) color_palette (" Paired ", 10))
The second category of palettes is called "sequential". This color mapping is appropriate when the data ranges from relatively low or noninterrupted values to relatively high or interesting values
# Gradually darkens the board
SNS. Palplot (SNS) color_palette (" Blues "))
To make it lighter, simply add '_r' after the color
# From dark to light plate
SNS. Palplot (SNS) color_palette (" Blues_r "))
The third kind of palette is called "diverging". This data is used for large values and low values and high values for interesting data. There is usually also a clear midpoint in the data. For example, if the temperature change is plotted from a reference point in time, it is best to use a scattered color map to show the relative decrease and relative increase.
A color palette for distinguishing
SNS. Palplot (SNS) color_palette (" BrBG ", 7))
We can also customize this divergent palette
A palette with a light color at the center
# SEP controls the width of the middle light tones
SNS. Palplot (SNS) diverging_palette (10, 220, sep = 90, n = 7))
A swatch with a dark color in the center
Ns. Palplot (SNS. Diverging_palette (255, 133, L =60, n=7, Center ="dark"))