pandas基础:数据可视化

绘图中常用的参数

  • Series.plot():series的index为横坐标,value为纵坐标
  • kind → line,bar,barh…(折线图,柱状图,柱状图-横…)
  • label → 图例标签,Dataframe格式以列名为label
  • style → 风格字符串,这里包括了linestyle(-),marker(.),color(g)
  • color → 颜色,有color指定时候,以color颜色为准
  • alpha → 透明度,0-1
  • use_index → 将索引用为刻度标签,默认为True
  • rot → 旋转刻度标签,0-360
  • grid → 显示网格,一般直接用plt.grid
  • xlim,ylim → x,y轴界限
  • xticks,yticks → x,y轴刻度值
  • figsize → 图像大小
  • title → 图名
  • legend → 是否显示图例,一般直接用plt.legend() 也可以 → plt.plot()
# 引入包
import pandas as pd
import numpy as np

线性图

# index 1000天的date类型数据,每天对应的数据随机生成
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.describe()

直接打印

ts.plot();

在这里插入图片描述

设置标题,和 数据的范围(数据值在-30至30之间)和 长宽比为4:3

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

在这里插入图片描述
多条线

# 生成数据
df = pd.DataFrame(np.random.randn(1000, 2), index=ts.index, columns=list('AB'))
df = df.cumsum()
# 绘制图像
df['I'] = np.arange(len(df))
df.plot(x='I', y=['A', 'B'])

在这里插入图片描述

柱状图

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

在这里插入图片描述
每类在数值中的分布

df.plot.bar(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.plot.hist(subplots=True, sharex=True, sharey=True)

在这里插入图片描述
每类的数值分布

df.plot.hist(alpha=0.5)

在这里插入图片描述
显示网格

df.plot.hist(stacked=True, bins=20, grid=True)

grid

密度图

# 数据生成
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['a'].plot.kde()
df.plot.kde()

在这里插入图片描述

散步图

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b');

在这里插入图片描述

# 数据生成
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.plot.scatter(x='a', y='b')

在这里插入图片描述

饼图

# 数据生成
s = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')
# 绘制
s.plot.pie(figsize=(6,6))

在这里插入图片描述

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

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值