Getting Started with Seaborn

Source: Internet
Author: User
Keywords seaborn seaborn tutorial seaborn python
I. Comparison between Seaborn and Matplotlib
Seaborn is a powerful extension of Matplotlib.

A case in point
Ask for a scatter plot of the length of the calyx and petal, and color to distinguish the species of flower

There are three varieties of flowers:


Define the color of each flower according to the type of flower

Color_map = dict (zip (iris) Name) unique (), [' blue ', 'green', 'red']))

Use Matplotlib to draw the picture
For species, Group in iris. Group by('Name'):
PLT. Scatter (group [' PetalLength], group [' SepalLength],
Color = color_map [species],
Alpha = 0.3, edgecolor = None,
Label = species)
PLT. Legend (frameon = True, the title = 'Name')
PLT. Xlabel (' petalLength ')
PLT. Ylabel (' sepalLength ')


Draw with Seaborn
Seaborn is much easier than matplotlib to draw a scatter plot with just one line of code.

Nss.lmplot ('PetalLength', 'SepalLength', Iris, Hue ='Name', fit_reg=False)

Second, Seaborn realizes histogram and density map
0x1 recalls the Matplotlib method
S1 = Series (np) random) randn (1000))
PLT. Hist (s1)
= 'kde' s1. The plot (kind)

0X2 draws histograms
Seaborn has a powerful approach: distplot, which supports a few parameters:

Bins: blocks of histogram
Hist: True means drawing histogram, and the default is True
Kde: True means to draw a density graph, and the default is True
Rug: Displays distribution, False by default, not displayed

SNS. Distplot (s1, hist = True, kde = True)

You can see the data distribution below

0x3 draws a density map
The density map can be drawn by directly passing in the data:

You can also specify the color with the color parameter:
SNS. Kdeplot (s1, shade = True, color = 'r')

tip
The PLT function can be called directly through NSS.plT


3. Seaborn realizes the bar chart and thermal diagram
0x1 data preparation
Seaborn provided a load_dataset method which can download data online as an experiment. This method was used to generate experiment data:

Load_dataset implementation source at https://github.com/mwaskom/seaborn/blob/master/seaborn/utils.py

PivotTable
Pivot (index='month', Columns ='year', values='passengers')

0x2 maps the heat map
Seaborn provided the heatMap method for plotting a heatmap:
The parameters annot=True, FMT ='d' can make each square display specific value in the thermal diagram:

0X2 draws a histogram
The abscissa of the bar chart is the year, and the ordinate is the sum of all passengers in the month of the year:
Firstly, the sum method is used to calculate the passenger sum of each year:
Where index is the year and VALUES is the sum of the passengers of the year
Seaborn provides the Barplot method with bar-bar plot, just specifying x and Y coordinates in the parameter:
SNS. Barplot (x = s.i ndex, y = s.v alues)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.