ch9_02 绘图和数据可视化

Jupyter notebook

使用pandas 和seaborn绘图

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

线型图

  • Series和DataFrame都有一个用于生成各类图表的plot方法。默认情况下,它们所生成的是线型图
s = pd.Series(np.random.randn(10).cumsum(),index=np.arange(0,100,10))
s.plot()
<matplotlib.axes._subplots.AxesSubplot at 0xb4a1ab0>

在这里插入图片描述

s
0    -0.502125
10    0.611011
20    0.924997
30    1.859573
40    4.276406
50    4.366946
60    1.946726
70    1.688532
80    0.742966
90    1.994725
dtype: float64
  • 该Series对象的索引会被传给matplotlib,并用以绘制X轴。可以通过use_index=False禁用该功能。X轴的刻度和界限可以通过xticks和xlim选项进行调节,Y轴就用yticks和ylim。

  • 参数列表如下:

  • DataFrame的plot方法会在一个subplot中为各列绘制一条线,并自动创建图例

df = pd.DataFrame(np.random.randn(10,4).cumsum(0),
                 columns=['A',"B",'C','D'],
                 index = np.arange(0,100,10))
df
ABCD
00.375814-0.1079050.285597-0.127976
100.107596-0.805818-0.507620-1.007167
20-0.594898-1.781004-1.514495-2.744035
30-0.793316-3.497805-0.961141-2.771882
40-0.647471-4.6911190.344920-1.757087
501.031137-5.6514190.801765-2.166651
600.899216-6.2057081.471705-0.539999
702.121010-5.4405311.715724-1.720697
802.958382-3.9587500.481963-1.820990
903.670376-4.223897-0.177123-1.446318
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0xb4dc230>

在这里插入图片描述

  • DataFrame还有一些用于对列进行灵活处理的选项,例如,是要将所有列都绘制到一个subplot中还是创建各自的subplot
  • 参数表

柱状图

  • 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='r',alpha=0.7)
<matplotlib.axes._subplots.AxesSubplot at 0xb72dad0>

在这里插入图片描述

# 有正数有负数
np.random.randn(10)
array([-1.07023238, -2.60305521, -0.34270297, -2.20261898,  0.0295722 ,
       -0.25381402,  0.10370884,  0.22665981, -0.00807395, -0.1277233 ])
# 只有正数
np.random.rand(10)
array([0.18845475, 0.26808236, 0.4911909 , 0.30446556, 0.27224037,
       0.6644953 , 0.780168  , 0.67096553, 0.93430181, 0.35273774])
  • 对于DataFrame,柱状图会将每一行的值分为一组,并排显示
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.3280680.6763360.8618670.756256
two0.4150730.2808040.1363880.426357
three0.2763420.9262640.4702090.517113
four0.6471340.5847310.9572540.740221
five0.2851880.6682810.0635780.354868
six0.6198280.2530860.8837060.069014
df.plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x154f3f0>

在这里插入图片描述

  • 设置stacked=True即可为DataFrame生成堆积柱状图,这样每行的值就会被堆积在一起
df.plot.barh(stacked=True, alpha=0.6)
<matplotlib.axes._subplots.AxesSubplot at 0x15a3b70>

在这里插入图片描述

  • 假设我们想要做一张堆积柱状图以展示每天各种聚会规模的数据点的百分比。我用read_csv将数据加载进来,然后根据日期和聚会规模创建一张交叉表:
tips = pd.read_csv('data/examples/tips.csv')
party_counts = pd.crosstab(tips['day'],tips['size'])
party_counts
size123456
day
Fri1161100
Sat253181310
Sun039151831
Thur1484513
tips[:10]
total_billtipsmokerdaytimesize
016.991.01NoSunDinner2
110.341.66NoSunDinner3
221.013.50NoSunDinner3
323.683.31NoSunDinner2
424.593.61NoSunDinner4
525.294.71NoSunDinner4
68.772.00NoSunDinner2
726.883.12NoSunDinner4
815.041.96NoSunDinner2
914.783.23NoSunDinner2
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 0xb916610>

在这里插入图片描述

party_counts.sum(1)#参数1是指对每一行求和
day
Fri     18
Sat     85
Sun     75
Thur    58
dtype: int64
  • 对于在绘制一个图形之前,需要进行合计的数据,使用seaborn可以减少工作量。用seaborn来看每天的小费比例
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 0xd925590>

在这里插入图片描述

  • seaborn的绘制函数使用data参数,它可能是pandas的DataFrame。其它的参数是关于列的名字。因为一天的每个值有多次观察,柱状图的值是tip_pct的平均值。绘制在柱状图上的黑线代表95%置信区间(可以通过可选参数配置)。

  • seaborn.barplot有颜色选项,使我们能够通过一个额外的值设置

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

在这里插入图片描述

  • 注意,seaborn已经自动修改了图形的美观度:默认调色板,图形背景和网格线的颜色。你可以用seaborn.set在不同的图形外观之间切换:
sns.barplot(x='tip_pct', y='day', hue='time', data=tips, orient='h')
sns.set(style="whitegrid")

在这里插入图片描述

直方图和密度图

  • 直方图(histogram)是一种可以对值频率进行离散化显示的柱状图。数据点被拆分到离散的、间隔均匀的面元中,绘制的是各面元中数据点的数量。再以前面那个小费数据为例:
tips['tip_pct'].plot.hist(bins=50)
<matplotlib.axes._subplots.AxesSubplot at 0xbb889f0>

在这里插入图片描述

  • 与此相关的一种图表类型是密度图,它是通过计算“可能会产生观测数据的连续概率分布的估计”而产生的。一般的过程是将该分布近似为一组核(即诸如正态分布之类的较为简单的分布)。因此,密度图也被称作KDE(Kernel Density Estimate,核密度估计)图。使用plot.kde和标准混合正态分布估计即可生成一张密度图
tips['tip_pct'].plot.density()
<matplotlib.axes._subplots.AxesSubplot at 0xbb68570>

在这里插入图片描述

  • seaborn的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='r')
<matplotlib.axes._subplots.AxesSubplot at 0xbd50c10>

在这里插入图片描述

散点图

macro = pd.read_csv('data/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')

在这里插入图片描述

  • 在探索式数据分析工作中,同时观察一组变量的散布图是很有意义的,这也被称为散布图矩阵(scatter plot matrix)。
sns.pairplot(trans_data,diag_kind='kde',plot_kws={'alpha':0.6})
<seaborn.axisgrid.PairGrid at 0xe58ec70>

在这里插入图片描述

  • plot_kws参数。它可以让我们传递配置选项到非对角线元素上的图形使用。对于更详细的配置选项,可以查阅seaborn.pairplot文档字符串。

分面网格和类型数据

  • 有多个分类变量的数据可视化的一种方法是使用小面网格。seaborn有一个有用的内置函数factorplot,可以简化制作多种分面图
sns.factorplot(x='day',y='tip_pct',hue='time',col='smoker',kind='bar',data=tips[tips.tip_pct<1])
d:\program filles\python\lib\site-packages\seaborn\categorical.py:3666: 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)





<seaborn.axisgrid.FacetGrid at 0xf261f50>

在这里插入图片描述

  • 除了在分面中用不同的颜色按时间分组,我们还可以通过给每个时间值添加一行来扩展分面网格:
sns.factorplot(x='day', y='tip_pct', row='time', col='smoker', kind='bar', data=tips[tips.tip_pct < 1])
d:\program filles\python\lib\site-packages\seaborn\categorical.py:3666: 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)





<seaborn.axisgrid.FacetGrid at 0xf4260f0>

在这里插入图片描述

  • factorplot支持其它的绘图类型,你可能会用到。例如,盒图(它可以显示中位数,四分位数,和异常值)就是一个有用的可视化类型
sns.factorplot(x='tip_pct',y='day',kind='box',data=tips[tips.tip_pct<0.5])
d:\program filles\python\lib\site-packages\seaborn\categorical.py:3666: 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)





<seaborn.axisgrid.FacetGrid at 0x103eeaf0>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值