绘图

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

pandas和seaborn绘图

折线图

s = pd.Series(np.random.randn(10).cumsum(),index=np.arange(0,100,10))
s
0     0.581758
10    0.927844
20    4.121972
30    3.849217
40    2.643392
50    1.699543
60    1.385943
70    0.623487
80    0.241257
90    0.363434
dtype: float64
s.plot()  #默认情况下plot()绘制的是折线图
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d17a9b0>

在这里插入图片描述

df = pd.DataFrame(np.random.randn(10,4).cumsum(0),columns=['A','B','C','D'],index=np.arange(0,100,10))
df
ABCD
0-0.4543740.3979380.3568420.071766
101.063938-0.9295800.4962210.142492
200.526858-0.747939-1.4057970.209039
301.022407-0.763073-2.2094880.071781
40-0.474689-0.782196-1.225337-0.438294
500.9034100.287945-0.837782-1.096535
600.473978-1.000205-0.728329-2.816553
700.327923-0.646799-0.635620-3.117912
800.350638-1.914932-1.410239-5.526775
900.446866-1.988524-1.112387-6.427741
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d2c54e0>

在这里插入图片描述

plot属性包含了不同绘图类型的方法族。df.plot()等价于df.plot.line().

柱状图

plot.bar()和plot.barh()可以分别绘制垂直和水平的柱状图。

Series或DataFrame的索引将会被用作x轴刻度(bar)或y轴刻度(barh)。

fig , axes = plt.subplots(2,1)
data = pd.Series(np.random.rand(16),index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0],color='k',alpha=0.7)
data.plot.barh(ax=axes[1],color='k',alpha=0.7)
#color='k'和alpha=0.7将柱子颜色设置为黑色,并将图像的填充色设置为部分透明。
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d37dc50>

在这里插入图片描述

df = pd.DataFrame(np.random.rand(6,4),index=['one','two','three','four','five','six'],columns=pd.Index(['A','B','C','D'],name='Genus'))
df
GenusABCD
one0.1163330.5475580.0573680.763959
two0.7906970.5466260.4087810.676374
three0.6048650.5570590.5306650.289824
four0.5672440.7984290.7828600.712815
five0.6280940.0056990.4395030.644901
six0.2226230.4425210.9477370.848191
df.plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d335278>

在这里插入图片描述

DataFrame的列名称“Genus”被用作了图例标题。通过传递stacked=Ture来生成堆积柱状图,使得每一行的值堆积在一起.

df.plot.barh(stacked=True,alpha=0.5)
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d478da0>

在这里插入图片描述

tips = pd.read_csv('examples/tips.csv')
party_counts = pd.crosstab(tips['day'],tips['size'])
party_counts
size123456
day
Fri1161100
Sat253181310
Sun039151831
Thur1484513
party_counts = party_counts.loc[: ,2:5]
party_counts
size2345
day
Fri16110
Sat5318131
Sun3915183
Thur48451
#标准化至和为1
party_pcts = party_counts.div(party_counts.sum(1),axis=0)
party_pcts
size2345
day
Fri0.8888890.0555560.0555560.000000
Sat0.6235290.2117650.1529410.011765
Sun0.5200000.2000000.2400000.040000
Thur0.8275860.0689660.0862070.017241
party_pcts.plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d640198>

在这里插入图片描述

import seaborn as sns
tips['tip_pct'] = tips['tip']/(tips['total_bill'] - tips['tip'])
tips.head()
total_billtipsmokerdaytimesizetip_pct
016.991.01NoSunDinner20.063204
110.341.66NoSunDinner30.191244
221.013.50NoSunDinner30.199886
323.683.31NoSunDinner20.162494
424.593.61NoSunDinner40.172069
sns.barplot(x ='tip_pct',y = 'day',data = tips,orient = 'h')
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5f29e6a0>

在这里插入图片描述

柱子的值是tip_pct的平均值。柱子上面画出的黑线代表的是95%的置信区间.

sns.barplot(x ='tip_pct',y = 'day',hue='time' ,data = tips,orient = 'h')
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5d69bac8>

在这里插入图片描述

使用seaborn.set在不同的绘图外观中进行切换.

sns.set(style='whitegrid')

直方图和密度图

tips['tip_pct'].plot.hist(bins=50)
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5f38a1d0>

在这里插入图片描述

tips['tip_pct'].plot.density()
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5f381278>

在这里插入图片描述

密度图也被称为内核密度估计图(KDE)。 plot.kde

distplot方法可以绘制直方图和连续密度估计。

comp1 = np.random.normal(0,1,size=200)

comp2 = np.random.normal(10,2,size=200)
values = pd.Series(np.concatenate([comp1,comp2]))
sns.distplot(values,bins=100,color='k')
<matplotlib.axes._subplots.AxesSubplot at 0x1ec5f5c7be0>

在这里插入图片描述

散点图或点图

点图或散点图可以用于检验两个一维数据序列之间的关系。

macro = pd.read_csv('examples/macrodata.csv')
data = macro[['cpi','m1','tbilrate','unemp']]
trans_data = np.log(data).diff().dropna()
trans_data[-5:]
cpim1tbilrateunemp
198-0.0079040.045361-0.3968810.105361
199-0.0219790.066753-2.2772670.139762
2000.0023400.0102860.6061360.160343
2010.0084190.037461-0.2006710.127339
2020.0088940.012202-0.4054650.042560

使用seaborn的regplot方法,绘制散点图。

sns.regplot('m1','unemp',data=trans_data)
plt.title('Changes in log %s versus log %s'%('m1','unemp'))
Text(0.5,1,'Changes in log m1 versus log unemp')

在这里插入图片描述

pairplot函数,在对角线上放置每个变量的直方图或密度估计值。

sns.pairplot(trans_data,diag_kind='kde',plot_kws={'alpha':0.2})
<seaborn.axisgrid.PairGrid at 0x1ec60d4add8>

在这里插入图片描述

分面网格和分类数据

使用分面网格是利用多种分组变量对数据进行可视化的方式。

seaborn拥有一个有效的内建函数factorplot,它可以简化多种分面绘图。

sns.factorplot(x='day',y='tip_pct',hue='time',col='smoker',kind='bar',data=tips[tips.tip_pct<1])
<seaborn.axisgrid.FacetGrid at 0x1ec612b2cc0>

在这里插入图片描述

#通过每个时间值添加一行来扩展分面网格
sns.factorplot(x='day',y='tip_pct',row='time',col='smoker',kind='bar',data=tips[tips.tip_pct<1])
<seaborn.axisgrid.FacetGrid at 0x1ec61483320>

在这里插入图片描述

factorplot支持其他可能有用的图类型,具体取决于显示的内容。

sns.factorplot(x='tip_pct',y='day',kind='box',data=tips[tips.tip_pct<0.5])
<seaborn.axisgrid.FacetGrid at 0x1ec61f40860>

在这里插入图片描述

小结: 使用pandas和seaborn入门一些基础数据的可视化知识。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值