机器学习数据科学包——pandas(三)

数据可视化

Pandas 的数据可视化使用 matplotlib 为基础组件。

%matplotlib inline
import pandas as pd
import numpy as np

线型图

Series 和 DataFrame 都提供了一个 plot 的函数,可以直接画出线形图。

1、Series
产生数据

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.describe()

# 运行结果
count    1000.000000
mean       25.654066
std        15.375182
min       -11.373822
25%        21.186677
50%        30.685201
75%        35.655332
max        49.113851
dtype: float64

直接使用plot 函数画出

ts.plot();

在这里插入图片描述
plot 函数的参数:
title:标题
style:颜色及线形,如‘r-’为红色实线,‘b–’为蓝色虚线
xlim:x轴的范围
ylim:y轴的范围
figsize:数据图形状大小

ts.plot(title='cumsum', style='r-',xlim=['1/1/2000', '1/1/2001'], ylim=[-30, 30], figsize=(4, 3));

在这里插入图片描述
2、DataFrame
产生数据

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.describe()

在这里插入图片描述
直接使用plot 函数画出

df.plot();

在这里插入图片描述
其他参数设置:
title:标题
figsize:数据图形状大小
subplots:是否采用子图分别绘制
sharex:子图x轴范围是否相同
sharey:子图y轴范围是否相同

df.plot(title='DataFrame cumsum', figsize=(4, 12), subplots=True, sharex=True, sharey=True);

在这里插入图片描述
更改坐标轴数据

df['I'] = np.arange(len(df))
df.plot(x='I', y=['A', 'C'])

在这里插入图片描述

柱状图

产生数据

df = pd.DataFrame(np.random.rand(10, 4), columns=['A', 'B', 'C', 'D'])

在这里插入图片描述
选取某一行数据进行绘制

df.loc[1].plot(kind='bar');
# 或df.loc[1].plot.bar();

在这里插入图片描述
将多组数据绘制在一张图中

df.plot.bar()

在这里插入图片描述
将各组数据重合在一起

df.plot.bar(stacked=True)

在这里插入图片描述
横向显示

df.plot.barh(stacked=True)

在这里插入图片描述

直方图

直方图是一种对值频率进行离散化的柱状图,数据点被分到离散的,间隔均匀的区间中,绘制各个区间中数据点的数据。

产生数据

df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
                   'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df = ts.cumsum()
df.describe()

# 运行结果
count     1000.000000
mean      9198.561144
std       8083.180361
min       -384.771918
25%        700.463067
50%       7839.405907
75%      16030.414941
max      25654.065887
dtype: float64

将三组数据绘制在一张图上,每组数据分为10个部分

df.plot.hist(bins=10);

在这里插入图片描述
部分数据被遮挡,可以调整透明度

df.plot.hist(alpha=0.5);

在这里插入图片描述
将各组数据以子图显示

df.plot.hist(subplots=True, sharex=True, sharey=True);

在这里插入图片描述

概率密度图

正态分布(高斯分布)就是一种自然界中广泛存在密度图。比如我们的身高,我们的财富,我们的智商都符合高斯分布。

将上文数据中的a组数据以密度图显示

df['a'].plot.kde();

在这里插入图片描述
三组数据同时绘制出

df.plot.kde();

在这里插入图片描述
平均值及方差

print(f'mean:\n{df.mean()}\n')
print(f'std:\n{df.std()}')

# 运行结果
mean:
a    0.989383
b    0.000745
c   -0.973448
dtype: float64

std:
a    0.972294
b    1.004547
c    0.982603
dtype: float64

带密度估计的规格化直方图

df['a'].plot.hist(bins=100, alpha=0.5, density=True)
df['a'].plot.kde(style='r-')

在这里插入图片描述

散布图

散布图是把所有的点画在同一个坐标轴上的图像,是观察两个一维数据之间关系的有效的手段。

产生数据

df = pd.DataFrame({'a': np.concatenate([np.random.normal(0, 1, 200), np.random.normal(6, 1, 200)]),
                  'b': np.concatenate([np.random.normal(10, 2, 200), np.random.normal(0, 2, 200)]),
                  'c': np.concatenate([np.random.normal(10, 4, 200), np.random.normal(0, 4, 200)])})
df.describe()

在这里插入图片描述
画出任意两组数据之间的散点图

df.plot.scatter(x='a', y='b');
df.plot.scatter(x='a', y='c');
df.plot.scatter(x='b', y='c')

在这里插入图片描述

饼图

1、Series
产生数据

s = pd.Series(10 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
s

# 运行结果
a    7.381159
b    8.247781
c    8.968931
d    0.529332
Name: series, dtype: float64

绘制饼图

s.plot.pie();

在这里插入图片描述
其他参数:
labels:各部分的名称
colors:各部分的颜色
autopct:占据的百分比
fontsize:字体大小
figsize:图形大小

s.plot.pie(labels=['AA', 'BB', 'CC', 'DD'], colors=['r', 'g', 'b', 'c'],
           autopct='%.2f', fontsize=20, figsize=(6, 6))

在这里插入图片描述
2、DataFrame
产生数据

df = pd.DataFrame(10 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])

在这里插入图片描述
绘制饼状图

df.plot.pie(subplots=True, figsize=(9, 4));

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值