Seaborn is a more advanced API package based on
Matplotlib, which makes drawing easier. In most cases,
Seaborn can make a very attractive graph. The datasets used in this example are all classic datasets provided by Seaborn, and the dataset file can be seen in GitHub. This blog only summarizes some for the convenience of bloggers to query. For details, you can see the official Seaborn API and example gallery. The official documents are well written.
1. set_ style( ) set( )
set_ Style () is used to set the theme. Seaborn has five preset themes: darkgrid, whitegrid, dark, white, and ticks. Default: darkgrid
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_ style("whitegrid")
plt.plot ( np.arange (10))
plt.show ()
Set() can be used to set the background, palette and so on by setting parameters, which is more commonly used.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set (style="white", palette="muted", color_ Codes = true) ාset() to set theme, palette is more commonly used
plt.plot ( np.arange (10))
plt.show ()
2. distplot( ) kdeplot( )
Distplot() is hist enhanced version, kdeplot() is density curve
import matplotlib.pyplot as plt
import seaborn as sns
Df_ iris = pd.read_ csv('../input/ iris.csv )
fig, axes = plt.subplots (1,2)
sns.distplot (df_ Iris ['petal length '], ax = axes [0], KDE = true, rug = true) ා KDE density curve rug marginal blanket
sns.kdeplot (df_ Iris ['petal length '], ax = axes [1], shade = true) × shade
plt.show ()
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set ( palette="muted", color_ codes=True)
rs = np.random.RandomState (10)
d = rs.normal (size=100)
f, axes = plt.subplots (2, 2, figsize=(7, 7), sharex=True)
sns.distplot (d, kde=False, color="b", ax=axes[0, 0])
sns.distplot (d, hist=False, rug=True, color="r", ax=axes[0, 1])
sns.distplot (d, hist=False, color="g", kde_ kws={"shade": True}, ax=axes[1, 0])
sns.distplot (d, color="m", ax=axes[1, 1])
plt.show ()
3. Boxplot ()
import matplotlib.pyplot as plt
import seaborn as sns
Df_ iris = pd.read_ csv('../input/ iris.csv )
sns.boxplot (x = df_ iris['class'],y = df_ iris['sepal width'])
plt.show ()
import matplotlib.pyplot as plt
import seaborn as sns
tips = pd.read_ csv('../input/ tips.csv )
sns.set (style = "ticks") ා set theme
sns.boxplot (x="day", y="total_ Bill ", hue = sex", data = tips, palette = "prgn"), palette palette
plt.show ()
4. joint distribution jointplot ()
tips = pd.read_ csv('../input/ tips.csv The correlation coefficient is shown in the upper right corner
sns.jointplot ("total_ bill", "tip", tips)
plt.show ()
tips = pd.read_ csv('../input/ tips.csv )
sns.jointplot ("total_ bill", "tip", tips, kind='reg')
plt.show ()
5. Heatmap ()
internal_ chars = ['full_ sq', 'life_ sq', 'floor', 'max_ floor', 'build_ year', 'num_ room', 'kitch_ sq', 'state', 'price_ doc']
corrmat = train[internal_ chars].corr()
f, ax = plt.subplots (figsize=(10, 7))
plt.xticks (rotation='90')
sns.heatmap (corrmat, square=True, linewidths=.5, annot=True)
plt.show ()
6. Scatter ()
f, ax = plt.subplots (figsize=(10, 7))
plt.scatter (x=train['full_ sq'], y=train['price_ doc'], c='r')
plt.xlim (0,500)
plt.show ()
7. Pointplot draws the relationship between variables
grouped_ df = train_ df.groupby ('floor')['price_ doc'].aggregate( np.median ).reset_ index()
plt.figure (figsize=(12,8))
sns.pointplot (grouped_ df.floor.values , grouped_ df.price_ doc.values , alpha=0.8, color=color[2])
plt.ylabel ('Median Price', fontsize=12)
plt.xlabel ('Floor number', fontsize=12)
plt.xticks (rotation='vertical') plt.show ()
8. pairplot( )
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_ csv("../input/ iris.csv ""
sns.set () ා use default color
sns.pairplot (data, hue = class) ා hue select the classification column
plt.show ()
import seaborn as sns
import matplotlib.pyplot as plt
iris = pd.read_ csv('../input/ iris.csv )
sns.pairplot (iris, vars=["sepal width", "sepal length"],hue='class',palette="husl")
plt.show ()
9. FacetGrid( )
import seaborn as sns
import matplotlib.pyplot as plt
tips = pd.read_ csv('../input/ tips.csv )
g = sns.FacetGrid (tips, col="time", row="smoker")
g = g.map( plt.hist , "total_ bill", color="r")
plt.show ()
10. barplot( )
f, ax= plt.subplots (figsize=(12,20))
#Original = H 'indicates horizontal display, and alpha indicates the depth of color
sns.barplot (y=group_ df.sub_ area.values , x=group_ df.price_ doc.values ,orient='h', alpha=0.8, color='red')
#Set the coordinate name and font size of Y-axis and x-axis
plt.ylabel ('price_ doc', fontsize=16)
plt.xlabel ('sub_ area', fontsize=16)
#Set the column subscript font of the X axis to be horizontal
plt.xticks (rotation='horizontal')
#Sets the font size of the y-axis subscript
plt.yticks (fontsize=15)
plt.show ()
Note: if the orientation is displayed vertically, remember y = group_ df.sub_ area.values , x=group_ df.price_ doc.values Change it