Abstract: This paper introduces Seaborn, a library based on
Matplotlib, which is easier to customize the mapping
Seaborn is actually a higher-level API encapsulation based on Matplotlib, which makes drawing easier. In most cases, Seaborn can make very attractive drawings, while Matplotlib can make drawings with more characteristics. Seaborn should be seen as a complement to Matplotlib, not a replacement.
How to install
Seaborn pip install seaborn
Raw data presentation (this is data from a family survey, preglngth - gestational circumference, birthward - the pregnant woman's first child, birthwgt_ Lb1 - infant weight in pounds, birthwgt_ OZ1 - infant weight (unit: Cup), ageprog - age of pregnant women at delivery
import pandas as pd
births = pd.read_ csv(' births.csv )
histogram
It has been used in the last article pandas.DataFrame.hist () to make histogram, now use seaborn.distplot () to make histogram and observe the difference
#Make a histogram for the prglngth column of the above table
import matplotlib.pyplot as plt
Import Seaborn as SNS ා note that once Seaborn is imported, the default mapping style of Matplotlib will be overwritten with Seaborn format
%Matplotlib inline: this command is required to draw in jupyter notebook
sns.distplot (births['prglngth'])
sns.plt.show ()
It can be seen that the biggest difference between the histogram made by Matplotlib and the histogram made by Matplotlib is that there is a density curve (KDE), which can be removed by setting parameters
sns.distplot (births['prglngth'], kde=False)
sns.plt.show ()
So what's the difference between pandas and Seaborn?
In fact, both of them use Matplotlib for drawing, but there are very different design differences
You can use pandas directly when you only need to draw a simple picture, but to make a more attractive and rich picture, you can use Seaborn
There are not too many parameters in the drawing function of pandas to adjust the drawing, so you must have a deep understanding of Matplotlib
Seaborn's mapping function provides a large number of parameters to adjust the graph, so you don't need to know much about Matplotlib
Seaborn's API: https://stanford.edu/~mwaskom/software/seaborn/api.html#style -frontend
#More configuration for the above figure
sns.set_ Style ('dark ') the picture uses black as the background color
sns.distplot (births ['prglngth '], KDE = false) ා do not display density curve
sns.axlabel ('birth number ',' frequency ') sets the coordinate meaning of x-axis and y-axis
sns.plt.show ()
Box diagram
#Take birthday as x-axis and ageprog as Y-axis to make a box diagram
sns.boxplot (x='birthord', y='agepreg', data=births)
sns.plt.show ()
Multivariate mapping
Seaborn can combine two or more variables at one time to make multiple contrast graphs. If there are n variables, a graph of n × n lattices will be made. For example, if there are two variables, four lattices will be generated. Each lattice is the contrast graph between two variables
var1 vs var1
var1 vs var2
var2 vs var1
var2 vs var2
The same two variables (VAR1 vs VAR1 and var2 vs var2) are displayed in histogram, and the different variables are displayed in scatter diagram (VAR1 vs var2 and var2 vs VAR1)
Note that there should be no Nan (missing data) in the data, otherwise an error will be reported
sns.pairplot (births, vars=['agepreg', 'prglngth','birthord'])
sns.plt.show ()
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.