Application of Seaborn

Source: Internet
Author: User
Keywords seaborn seaborn tutorial seaborn python
Matplotlib is Python's main drawing library. Although Matplotlib is powerful, it is complex in its own right and often requires a lot of tweaking to refine the chart.
Seaborn is a very useful visualization package from Stanford University. To control the appearance of Matplotlib charts, Seaborn module comes with many custom themes and advanced interfaces.
1. First look at Seaborn's own case and feel the difference.
(1) Matplotlib drawing
code:
import numpy as np
import matplotlib as mpl
import  matplotlib.pyplot  as plt
np.random.seed (sum(map(ord, "aesthetics")))
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)
(2) Seaborn drawing
The same picture code is relatively simple:
import seaborn as sns
sinplot()
Seaborn's default light gray background and white network line are inspired by Matplotlib, but they are more soft than Matplotlib. We have found that network lines are very useful for spreading information, and in almost all cases, people prefer graphs to tables. By default, the form of a white gray grid avoids being too harsh. In the case of multi-faceted drawing, the network form is quite advantageous and provides a drawing structure, which is very important for some complex tools in the module.
2. The application of Seaborn
########Import module
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_ style('darkgrid')
########Import data
names = [
'mpg'
,  'cylinders'
,  'displacement'
,  'horsepower'
,  'weight'
,  'acceleration'
,  'model_ year'
,  'origin'
,  'car_ name'
]
df =  pd.read_ csv(" http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data ", sep='\s+', names=names)
df['maker'] =  df.car_ name.map (lambda x: x.split()[0])
df.origin  = df.origin.map ({1: 'America', 2: 'Europe', 3: 'Asia'})
Df= df.applymap (lambda x:  np.nan  if x == '?' else x).dropna()
df['horsepower'] =  df.horsepower.astype (float)
df.head ()
#1. General drawing functions: factorplot and facegrid
#(1) Draw the model according to two dimension variables_ The graph of year and mpg
sns.factorplot (data=df, x="model_ year", y="mpg")
#By adding kind parameter, the line chart is changed into column chart
sns.factorplot (data=df, x="model_ year", y="mpg",kind="bar")
#(2) You can draw different diagrams according to the third dimension
sns.factorplot (data=df, x="model_ year", y="mpg", col="origin")
#(3) Various graphs
#Column chart, add density function
g =  sns.FacetGrid (df, col="origin")
g.map( sns.distplot , "mpg")
#Scatter plot
g =  sns.FacetGrid (df, col="origin")
g.map( plt.scatter , "horsepower", "mpg")
#Add linear regression line
plt.xlim (0, 250)
plt.ylim (0, 60)
#KDE contour
df['tons'] = ( df.weight/2000 ).astype(int)
g =  sns.FacetGrid (df, col="origin", row="tons")
g.map( sns.kdeplot , "horsepower", "mpg")
plt.xlim (0, 250)
plt.ylim (0, 60)
#2. Matrix diagram pairplot and pairgrid
#Do not add regression lines
g =  sns.pairplot (df["mpg", "horsepower", "weight", "origin"], hue="origin", diag_ kind="hist")
for ax in g. axes.flat :
plt.setp ( ax.get_ xticklabels(), rotation=45)
#Add regression
g =  sns.PairGrid (df["mpg", "horsepower", "weight", "origin"], hue="origin")
g.map_ upper( sns.regplot )
g.map_ lower( sns.residplot )
g.map_ diag( plt.hist )
for ax in g. axes.flat :
plt.setp ( ax.get_ xticklabels(), rotation=45)
g.add_ legend()
g.set(alpha=0.5)
#3. Joint plot and jointgrid
#KDE contour map
sns.jointplot ("mpg", "horsepower", data=df, kind='kde')
#Add a graph with linear regression
sns.jointplot ("horsepower", "mpg", data=df, kind="reg")
#Add graph of multiple regression
g =  sns.JointGrid (x="horsepower", y="mpg", data=df)
g.plot_ joint( sns.regplot , order=2)
g.plot_ marginals( sns.distplot )
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.