Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

Code
import seaborn as sns
print(sns.__version__)
Code
# Import seaborn
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

# Apply the default theme
#sns.set_theme()

# Load an example dataset
tips = sns.load_dataset("tips")
tips.head()

1 Scatter Plot

Code
sns.scatterplot(data=tips,x='tip',y='total_bill')

1.1 color by group

Code
sns.scatterplot(data=tips,x='tip',y='total_bill',hue='sex')

1.2 size by group

Code
sns.scatterplot(data=tips,x='tip',y='total_bill',size='size')

2 line Plot

Code
dowjones= sns.load_dataset("dowjones")
dowjones.head()
Code
sns.lineplot(data=dowjones,x='Date',y='Price')

2.1 color by group

Code
import random
from siuba import _, mutate, filter, group_by, summarize,show_query
from siuba import *

dowjones2=dowjones>>mutate(type='old')

dowjones3=dowjones>>mutate(Price=_.Price+random.random()*200,type='new')

dowjones4=pd.concat([dowjones2, dowjones3], ignore_index = True)>> arrange(_.Date)
Code
dowjones4.head()
Code
sns.lineplot(data=dowjones4,x='Date',y='Price',hue='type')

3 histogram

Code
sns.histplot(data=tips,x='tip')

3.1 color by group

Code
sns.histplot(data=tips,x='tip',hue='sex',multiple="dodge")

4 bar chart

Code
sns.barplot(data=tips,x='sex',y='tip',errorbar=None)

4.1 show number

Code
ax=sns.barplot(data=tips,x='sex',y='tip',errorbar=None)

for i in ax.containers:
    ax.bar_label(i,)

4.2 horizontal bar plot

Code
ax=sns.barplot(data=tips,y='sex',x='tip',errorbar=None,orient = 'h')
plt.show()

5 box plot

Code
sns.boxplot(data=tips,x='day',y='tip')

5.1 color by group

Code
sns.boxplot(data=tips,x='day',y='tip',hue='sex')

6 strip plot

Code
sns.stripplot(data=tips,x='day',y='tip')

6.1 color by group

Code
sns.stripplot(data=tips,x='day',y='tip',hue='sex',dodge=True)

join plot

Code
sns.jointplot(data=tips,x='total_bill',y='tip',kind='reg')

7 Facet plot

Code
g = sns.FacetGrid(data=tips, col="day", hue="sex")

g.map_dataframe(sns.scatterplot, x="total_bill", y="tip")

g.add_legend()

make 2 plot per column

Code
g = sns.FacetGrid(data=tips, col="day",col_wrap=2, hue="sex")

g.map_dataframe(sns.scatterplot, x="total_bill", y="tip")

g.add_legend()

sub plot

Code
#sns.set()

#define plotting region (1 rows, 2 columns)
fig, axes = plt.subplots(1, 2)


sns.boxplot(data=tips,x='day',y='tip',hue='sex',ax=axes[0])
sns.boxplot(data=tips,x='day',y='tip',ax=axes[1])

8 chinese 显示中文 in Mac

Code
# add following line
plt.rcParams['font.family'] = ['Arial Unicode MS'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
sns.set_style('whitegrid',{'font.sans-serif':['Arial Unicode MS','Arial']})

9 title,size,x y name

9.1 add title

Code
df = sns.load_dataset("tips")

ax=sns.boxplot(x = "day", y = "total_bill", data = df)

ax.set_title("tips box plot ")

9.2 adjust size

Code
plt.clf()

plt.figure(figsize=(10, 6))

ax=sns.boxplot(x = "day", y = "total_bill", data = df)
ax.set_title("tips box plot ")

plt.show()

9.3 change x y name

Code
ax=sns.boxplot(x = "day", y = "total_bill", data = df)
ax.set_title("tips box plot ")
ax.set(xlabel='x-axis label', ylabel='y-axis label')

10 applying themes

11 Save plot

Code
import seaborn as sns

df = sns.load_dataset("tips")

plt.clf()

plt.style.use('default')

sns.boxplot(x = "day", y = "total_bill", data = df)

# Save the plot with desired size
plt.savefig("output.png", dpi=100, bbox_inches="tight")

12 Animation plot

Code
from celluloid import Camera
Code
from celluloid import Camera
from matplotlib import pyplot as plt

fig = plt.figure()

camera = Camera(fig)

a=sns.lineplot(data=dowjones4,x='Date',y='Price',hue='type')

hands, labs = a.get_legend_handles_labels()

new_data=dowjones4.sample(50, random_state=42)

new_data=new_data.sort_values(by=['Date'], ascending=True)

for i in (new_data["Date"]):
  data=dowjones4.query('Date <= @i')
  #print(data)
  sns.lineplot(data=data,x='Date',y='Price',hue='type')
  plt.legend(handles=hands, labels=labs)
  camera.snap()

animation = camera.animate()
Code
from IPython.display import HTML
HTML(animation.to_html5_video())

13 reference:

https://seaborn.pydata.org/index.html

https://www.youtube.com/watch?v=ooqXQ37XHMM

Back to top