代码中数据集下载链接:https://github.com/jsusu/Seaborn_data_visualization/tree/master/seaborn_data
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
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)
sinplot()

sns.set()
sinplot()

sns.set_style("whitegrid")
data = np.random.normal(size=(20,6)) + np.arange(6)/2
sns.boxplot(data=data)

sns.set_style("dark")
sinplot()

sns.set_style("white")
sinplot()

sns.set_style("ticks")
sinplot()

sinplot()
sns.despine()

sns.violinplot(data)
sns.despine(offset=10)

sns.set_style("whitegrid")
sns.boxplot(data=data,palette="deep")
sns.despine(left=True)

with sns.axes_style("darkgrid"):
plt.subplot(211)
sinplot()
plt.subplot(212)
sinplot(-1)

sns.set()
sns.set_context("paper")
plt.figure(figsize=(8,6))
sinplot()

sns.set_context("talk")
plt.figure(figsize=(8,6))
sinplot()

sns.set_context("poster")
plt.figure(figsize=(8,6))
sinplot()

sns.set_context("notebook",font_scale=1.5,rc={"lines.linewidth":2.5})
sinplot()

sns.set(rc={"figure.figsize":(6,6)})
current_palette = sns.color_palette()
sns.palplot(current_palette)

sns.palplot(sns.color_palette("hls",8))

data = np.random.normal(size=(20,8))+np.arange(8)/2
sns.boxplot(data=data,palette=sns.color_palette("hls",8))

sns.palplot(sns.hls_palette(8,l=0.3,s=0.8))

sns.palplot(sns.color_palette("Paired",10))

plt.plot([0,1],[0,1],sns.xkcd_rgb["pale red"],lw=3)
plt.plot([0,1],[0,2],sns.xkcd_rgb["medium green"],lw=3)
plt.plot([0,1],[0,3],sns.xkcd_rgb["denim blue"],lw=3)
[]

sns.palplot(sns.color_palette("Blues"))

sns.palplot(sns.color_palette("Blues_r"))

sns.palplot(sns.color_palette("cubehelix",8))

sns.palplot(sns.cubehelix_palette(8,start=0.5,rot=-0.75))

sns.palplot(sns.cubehelix_palette(8,start=0.75,rot=-0.150))

sns.palplot(sns.light_palette("green"))

sns.palplot(sns.dark_palette("purple"))

sns.palplot(sns.dark_palette("purple",reverse=True))

x, y = np.random.multivariate_normal([0,0],[[1,-0.5],[-0.5,1]], size=300).T
pal = sns.dark_palette("green",as_cmap=True)
sns.kdeplot(x, y,cmap=pal)

sns.palplot(sns.light_palette((210,90,60), input="husl"))

from scipy import stats, integrate
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x = np.random.normal(size=100)
sns.distplot(x, kde=False)

sns.distplot(x, bins=20,kde=False)

x = np.random.gamma(6, size=200)
sns.distplot(x,kde=False,fit=stats.gamma)

mean, cov = [0,1],[(1,0.5),(0.5,1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x","y"])
df
|
x |
y |
0 |
2.190873 |
2.902961 |
1 |
0.387901 |
3.441322 |
2 |
-1.304909 |
0.586173 |
3 |
-0.016867 |
0.907323 |
4 |
0.284953 |
1.189304 |
... |
... |
... |
195 |
-0.804338 |
0.139381 |
196 |
1.674393 |
2.735944 |
197 |
-1.237634 |
0.002766 |
198 |
-1.044683 |
0.482758 |
199 |
-0.890160 |
0.042753 |
200 rows × 2 columns
sns.jointplot(x="x",y="y",data=df)

x, y = np.random.multivariate_normal(mean, cov,1000).T
with sns.axes_style("white"):
sns.jointplot(x=x,y=y, kind="hex", color="k")

iris = sns.load_dataset("iris")
sns.pairplot(iris)

sns.set(color_codes=True)
np.random.seed(sum(map(ord, "regression")))
tips = sns.load_dataset("tips")
tips.head()
|
total_bill |
tip |
sex |
smoker |
day |
time |
size |
0 |
16.99 |
1.01 |
Female |
No |
Sun |
Dinner |
2 |
1 |
10.34 |
1.66 |
Male |
No |
Sun |
Dinner |
3 |
2 |
21.01 |
3.50 |
Male |
No |
Sun |
Dinner |
3 |
3 |
23.68 |
3.31 |
Male |
No |
Sun |
Dinner |
2 |
4 |
24.59 |
3.61 |
Female |
No |
Sun |
Dinner |
4 |
sns.regplot(x="size",y="tip",data=tips, x_jitter=0.5)

sns.set(style="whitegrid",color_codes=True)
np.random.seed(sum(map(ord, "categorical")))
titanic = sns.load_dataset("titanic")
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")
sns.stripplot(x="day",y="total_bill",data=tips)
survived pclass sex age sibsp parch fare embarked class \
0 0 3 male 22.0 1 0 7.2500 S Third
1 1 1 female 38.0 1 0 71.2833 C First
2 1 3 female 26.0 0 0 7.9250 S Third
3 1 1 female 35.0 1 0 53.1000 S First
4 0 3 male 35.0 0 0 8.0500 S Third
who adult_male deck embark_town alive alone
0 man True NaN Southampton no False
1 woman False C Cherbourg yes False
2 woman False NaN Southampton yes True
3 woman False C Southampton yes False
4 man True NaN Southampton no True

sns.stripplot(x="day",y="total_bill",data=tips, jitter=True)

sns.swarmplot(x="day",y="total_bill",data=tips)

sns.swarmplot(x="day",y="total_bill",hue="sex", data=tips)

sns.swarmplot(x="day",y="total_bill",hue="time", data=tips)


sns.boxplot(orient="h", data=tips)

sns.violinplot(y="day", x="total_bill", hue="time", data=tips)

sns.violinplot(x="day", y="total_bill",split=True, hue="sex", data=tips)

sns.violinplot(x="day", y="total_bill", inner=None, data=tips)
sns.swarmplot(x="day", y="total_bill", color="w",alpha=0.5, data=tips)

sns.barplot(x="sex",y="survived", hue="class", data=titanic)

sns.pointplot(x="sex",y="survived", hue="class", data=titanic)

sns.pointplot(x="class",y="survived",hue="sex",data=titanic,
palette={"male":"g","female":"m"},
markers=["^","o"],linestyles=["-","--"])

sns.factorplot(x="day", y="total_bill",hue="smoker",data=tips)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/categorical.py:3669: UserWarning: The `factorplot` function has been renamed to `catplot`. The original name will be removed in a future release. Please update your code. Note that the default `kind` in `factorplot` (`'point'`) has changed `'strip'` in `catplot`.
warnings.warn(msg)

sns.factorplot(x="day", y="total_bill",hue="smoker",data=tips,kind="bar")
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/categorical.py:3669: UserWarning: The `factorplot` function has been renamed to `catplot`. The original name will be removed in a future release. Please update your code. Note that the default `kind` in `factorplot` (`'point'`) has changed `'strip'` in `catplot`.
warnings.warn(msg)

sns.factorplot(x="day", y="total_bill",col="time",hue="smoker",data=tips,kind="swarm")
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/categorical.py:3669: UserWarning: The `factorplot` function has been renamed to `catplot`. The original name will be removed in a future release. Please update your code. Note that the default `kind` in `factorplot` (`'point'`) has changed `'strip'` in `catplot`.
warnings.warn(msg)

sns.factorplot(x="time", y="total_bill",hue="smoker",data=tips,kind="box",
col="day",size=4,aspect=0.5)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/categorical.py:3669: UserWarning: The `factorplot` function has been renamed to `catplot`. The original name will be removed in a future release. Please update your code. Note that the default `kind` in `factorplot` (`'point'`) has changed `'strip'` in `catplot`.
warnings.warn(msg)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/categorical.py:3675: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
warnings.warn(msg, UserWarning)

tips.head()
|
total_bill |
tip |
sex |
smoker |
day |
time |
size |
0 |
16.99 |
1.01 |
Female |
No |
Sun |
Dinner |
2 |
1 |
10.34 |
1.66 |
Male |
No |
Sun |
Dinner |
3 |
2 |
21.01 |
3.50 |
Male |
No |
Sun |
Dinner |
3 |
3 |
23.68 |
3.31 |
Male |
No |
Sun |
Dinner |
2 |
4 |
24.59 |
3.61 |
Female |
No |
Sun |
Dinner |
4 |
g = sns.FacetGrid(tips,col="time")

g = sns.FacetGrid(tips,col="time")
g.map(plt.hist, "tip")

g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip",alpha=0.7)
g.add_legend()

g = sns.FacetGrid(tips, row="smoker",col="time", margin_titles=True)
g.map(sns.regplot, "size","total_bill",color="0.3",
fit_reg=True,
x_jitter=0.1)

g = sns.FacetGrid(tips,col="day",size=4,aspect=0.5)
g.map(sns.barplot, "sex", "total_bill")
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/axisgrid.py:243: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
warnings.warn(msg, UserWarning)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/axisgrid.py:728: UserWarning: Using the barplot function without specifying `order` is likely to produce an incorrect plot.
warnings.warn(warning)

from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(order_days)
g = sns.FacetGrid(tips,row="day",row_order=ordered_days,
size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")
CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/axisgrid.py:243: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
warnings.warn(msg, UserWarning)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/axisgrid.py:728: UserWarning: Using the boxplot function without specifying `order` is likely to produce an incorrect plot.
warnings.warn(warning)

pal = dict(Lunch="seagreen",Dinner="gray")
g = sns.FacetGrid(tips,hue="time",palette=pal,height=5)
g.map(plt.scatter, "total_bill","tip", s=50,alpha=0.7,linewidth=0.5,edgecolor="white")
g.add_legend()

g = sns.FacetGrid(tips,hue="sex",palette="Set1",size=5,
hue_kws={"marker":["^","v"]})
g.map(plt.scatter, "total_bill","tip", s=100,alpha=0.7,
linewidth=0.5,edgecolor="white")
g.add_legend()

with sns.axes_style("white"):
g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True,
size=2.5)
g.map(plt.scatter, "total_bill","tip",color="#334488",edgecolor="white",lw=0.5)
g.set_axis_labels("Total bill(US Dollars)", "Tip")
g.set(xticks=[10,30,50], yticks=[2,6,10])
g.fig.subplots_adjust(wspace=0.2, hspace=0.2)
/Users/susu/opt/anaconda3/envs/data_analysis/lib/python3.7/site-packages/seaborn/axisgrid.py:243: UserWarning: The `size` parameter has been renamed to `height`; please update your code.
warnings.warn(msg, UserWarning)

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)

g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)

g = sns.PairGrid(iris,hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()

g = sns.PairGrid(iris,hue="species",
vars=["sepal_length","sepal_width"])
g.map(plt.scatter)

g = sns.PairGrid(tips,hue="size",palette="GnBu_d")
g.map(plt.scatter,s=50,edgecolor="white")
g.add_legend()

%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
np.random.seed(0)
sns.set()
uniform_data = np.random.rand(3,3)
print(uniform_data)
heatmap = sns.heatmap(uniform_data)
[[0.11402723 0.39613034 0.86002087]
[0.10145661 0.06316734 0.54688621]
[0.9487925 0.82667995 0.85134329]]

ax = sns.heatmap(uniform_data,vmin=0.2,vmax=0.5)

normal_data = np.random.randn(3,3)
print(normal_data)
ax = sns.heatmap(normal_data,center=0)
[[-0.51964495 -0.55467919 -1.51977748]
[-0.95065924 -0.14189382 -0.75598294]
[ 0.54360754 0.35617477 -0.92938628]]

flights = sns.load_dataset("flights")
flights.head()
|
year |
month |
passengers |
0 |
1949 |
January |
112 |
1 |
1949 |
February |
118 |
2 |
1949 |
March |
132 |
3 |
1949 |
April |
129 |
4 |
1949 |
May |
121 |
flights = flights.pivot("month","year","passengers")
print(flights)
ax = sns.heatmap(flights)
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 \
month
January 112 115 145 171 196 204 242 284 315 340 360
February 118 126 150 180 196 188 233 277 301 318 342
March 132 141 178 193 236 235 267 317 356 362 406
April 129 135 163 181 235 227 269 313 348 348 396
May 121 125 172 183 229 234 270 318 355 363 420
June 135 149 178 218 243 264 315 374 422 435 472
July 148 170 199 230 264 302 364 413 465 491 548
August 148 170 199 242 272 293 347 405 467 505 559
September 136 158 184 209 237 259 312 355 404 404 463
October 119 133 162 191 211 229 274 306 347 359 407
November 104 114 146 172 180 203 237 271 305 310 362
December 118 140 166 194 201 229 278 306 336 337 405
year 1960
month
January 417
February 391
March 419
April 461
May 472
June 535
July 622
August 606
September 508
October 461
November 390
December 432

ax = sns.heatmap(flights,
annot=True,
fmt="d",
linewidths=0.5,
cmap="YlGnBu",
cbar=False,
)

End:如果你看到了这里,那么一定是将这一整篇都阅读喽,恭喜你,基本的seaborn已经学会了哦~~~