Seaborn案例展示

Seabron与matplotlib的对比

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


x = np.linspace(0,10,500)
y = np.cumsum(np.random.randn(500,6),axis=0)

with plt.style.context('classic'):
    plt.plot(x,y)
    plt.legend("ABCDEF",ncol=2,loc='upper left')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yhmzIecn-1628084478633)(output_1_0.png)]

import seaborn as sns

x = np.linspace(0,10,500)
y = np.cumsum(np.random.randn(500,6),axis=0)

sns.set()
plt.figure(figsize=(10,6))
plt.plot(x,y)
plt.legend("ABCDEF", ncol=2, loc='upper left')
<matplotlib.legend.Legend at 0x2ca875b1d48>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QTnjgPTK-1628084478637)(output_2_1.png)]

x = ['G1','G2','G3','G4','G5']
y = 2 * np.arange(1,6)

plt.figure(figsize=(8,4))
plt.barh(x,y,align='center',height=0.5,alpha=0.8,color='blue')
plt.tick_params(axis='both',labelsize=13)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i53B0bq3-1628084478639)(output_3_0.png)]

 np.arange(1,6)
    
array([1, 2, 3, 4, 5])
plt.figure(figsize=(8,4))
x = ['G1','G2','G3','G4','G5']
y = 2 * np.arange(0,5,1)
sns.barplot(y,x,linewidth=5)
<matplotlib.axes._subplots.AxesSubplot at 0x2ca87886f88>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wbLZWRIC-1628084478642)(output_5_1.png)]

sns.barplot? # 查看参数

iris = sns.load_dataset('iris')

iris.head()

sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

sns.pairplot(data=iris,hue='species')
<seaborn.axisgrid.PairGrid at 0x2ca883d8308>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BlcESN8Z-1628084478644)(output_8_1.png)]

线形图


df = pd.DataFrame(np.random.randn(1000,4).cumsum(axis=0),columns=list('ABCD'),index=np.arange(1000))
df.head()
ABCD
0-1.5953141.1708572.677344-0.405985
1-0.390817-0.2238253.907567-0.536584
20.040567-1.5664356.0476360.590226
3-0.5859280.1543615.7632130.066904
4-0.4623032.2552278.420961-0.527309
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x2ca88ae7908>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Cvw3Qow-1628084478647)(output_11_1.png)]

df = pd.DataFrame()
df.plot?

df2 = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
df2
abcd
00.2483920.4239400.5808170.239191
10.8931460.0550150.6044950.698673
20.4608490.1236190.2267230.655506
30.0491880.5606310.3236720.663870
40.6186080.6829030.1807600.885659
50.2364770.0072450.6768000.602075
60.5195450.6702980.3651680.920002
70.9374500.1511580.3284800.605270
80.0301910.9912960.6086420.105120
90.3223130.2345760.5936750.775744

多组数据图

df2.plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x2ca886ae148>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LArhkaJm-1628084478648)(output_16_1.png)]

多组数据累加竖图

df2.plot.bar(stacked=True)
<matplotlib.axes._subplots.AxesSubplot at 0x2ca886a3788>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ozKQKXoh-1628084478650)(output_18_1.png)]

df2.plot.barh(stacked=True)
<matplotlib.axes._subplots.AxesSubplot at 0x2ca888509c8>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dJy9eWEq-1628084478653)(output_19_1.png)]

直方图和密度图

df4 = pd.DataFrame({'A':np.random.randn(1000)-3,'B':np.random.randn(1000),'C':np.random.randn(1000)+3})
df4.head()
ABC
0-1.9943340.7581062.075204
1-1.467124-1.9096431.627641
2-1.833122-1.1333394.232661
3-3.332893-1.1983352.857277
4-3.581459-0.1313181.888054
  • 普通直方图
df4.plot.hist(bins=50)
<matplotlib.axes._subplots.AxesSubplot at 0x2ca89062308>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ud8DCUYx-1628084478654)(output_23_1.png)]

  • 累计直方图
df4['A'].plot.hist(cumulative=True)
<matplotlib.axes._subplots.AxesSubplot at 0x2ca87dca408>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fRicRaES-1628084478656)(output_25_1.png)]

df4['A'].plot(kind='kde')
<matplotlib.axes._subplots.AxesSubplot at 0x2ca88e0f508>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wlRO6HBr-1628084478657)(output_26_1.png)]

  • 差分
df = pd.DataFrame(np.random.randn(1000,4).cumsum(axis=0),columns=list('ABCD'),index=np.arange(1000))
df.head()
ABCD
0-0.346780-1.407473-1.371878-0.787592
10.179860-0.871498-2.049004-0.465820
2-0.3630720.280543-3.8306330.676657
3-0.029469-0.637807-4.507969-1.133796
40.1520411.118945-4.432778-2.893848
df.diff().hist(bins=50,color='r')
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8905A6C8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA88F7E588>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8B0A7DC8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8B0DC708>]],
      dtype=object)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XNhZcBz9-1628084478660)(output_30_1.png)]

散点图

housing = pd.read_csv(r'G:\BaiduNetdiskDownload\02-Python基础+数据科学入门训练营-【官网最新】\00-资料\00-资料\代码及数据资料合集\03代码及数据资料合集\课程代码及数据\13 第13章 Matplotlib库\housing.csv')
housing.head()
longitudelatitudehousing_median_agetotal_roomstotal_bedroomspopulationhouseholdsmedian_incomemedian_house_valueocean_proximity
0-122.2337.8841.0880.0129.0322.0126.08.3252452600.0NEAR BAY
1-122.2237.8621.07099.01106.02401.01138.08.3014358500.0NEAR BAY
2-122.2437.8552.01467.0190.0496.0177.07.2574352100.0NEAR BAY
3-122.2537.8552.01274.0235.0558.0219.05.6431341300.0NEAR BAY
4-122.2537.8552.01627.0280.0565.0259.03.8462342200.0NEAR BAY

基于地理数据人口\房价可视化

# 圆的半径大小代表每一个区域人口数量(S),颜色代表价格(C),用预定义的jet标可视化
with sns.axes_style('white'):
    housing.plot(kind='scatter',x='longitude',y='latitude',alpha=0.6,
                s=housing['population']/100,label='population',c='median_house_value',cmap='jet',
                colorbar=True,figsize=(12,8))
    plt.legend()
    plt.axis([-123,-113.5,32,43])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EX4fDg9r-1628084478661)(output_35_0.png)]

housing.plot(kind='scatter',x='median_income',y='median_house_value',alpha=0.8)
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.





<matplotlib.axes._subplots.AxesSubplot at 0x2ca8bb9f108>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MJQWTOUR-1628084478664)(output_36_2.png)]


df = pd.DataFrame(np.random.randn(1000,4).cumsum(axis=0),columns=list("ABCD"),index=np.arange(1000))
df.head()
ABCD
0-0.032738-1.2947301.365266-0.244759
1-1.0183300.3460971.765546-1.483448
2-1.660975-0.424961-0.627905-0.589975
3-0.8234132.0133520.374114-1.578334
40.2216463.8871251.166699-2.109392
df.plot(subplots=True,figsize=(6,16))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8BB8F148>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8BD7AFC8>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8B8A8648>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8B8119C8>],
      dtype=object)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-voxlVy49-1628084478665)(output_38_1.png)]

df.plot(subplots=True,layout=(2,2),figsize=(16,6),sharex=False)
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8BFF3608>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8C0005C8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8CC8D608>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000002CA8CCBEA88>]],
      dtype=object)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FuHZR7my-1628084478666)(output_39_1.png)]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wency(王斯-CUEB)

我不是要饭的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值