Python数据分析之Pandas入门(下)

前言

本系列共有三篇文章,依次按照pandas数据类型及其结构、内置模块对数据处理功能、可视化工具以及形如房价预测的案例分析内容展开介绍。参考自书籍《Python for Data Analysis(Second Edition)》,本篇文章的代码均已测试通过,数据集下载详见【资源】。
📢注意:代码文件应和解压后的数据及文件夹在同一目录下才能相对路径引用到,当然也可使用绝对路径。


八、绘图与可视化

IPython中执行 %matplotlib

import numpy as np
import pandas as pd
PREVIOUS_MAX_ROWS = pd.options.display.max_rows
pd.options.display.max_rows = 20
np.random.seed(12345)
import matplotlib.pyplot as plt
import matplotlib
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)

1.图片与子图

fig = plt.figure()
ax3 = fig.add_subplot(2, 2, 3)
# 'k--' 绘制黑色分段线的style选项
plt.plot(np.random.randn(50).cumsum(), 'k--') 
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)

# 绘制直方图 -> hist,设置显示字体大小、颜色、画布分辨率
_ = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
# 绘制散点图 -> scatter
ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30))
# 在最后一个子图绘制
plt.plot(np.random.randn(50).cumsum(), 'k--') 
plt.close('all')

输出为:
子图

  • pyplot.subplot 选项
参数描述
nrows子图的行数
ncols子图的列数
sharex所有子图使用相同的x轴刻度(调整xlim会影响所有子图)
sharey所有子图使用相同的y轴刻度(调整ylim会影响所有子图)
subplot_kw传入add_subplot的关键字参数字典,用于生成子图
**fig_kw在生成图片时使用的额外的关键字参数,例如plt.subplots(2, 2, figsize=(8, 6))
  • 调整子图周围的间距(Adjusting the spacing around subplots)

subplots(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)


①颜色、标记与线性类型

from numpy.random import randn
plt.plot(randn(30).cumsum(), color='k', linestyle='dashed', marker='o')

data = np.random.randn(30).cumsum()
plt.plot(data, 'k--', label='Default')
plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post')
plt.legend(loc='best')      # 图例标签显示位置在顶部

输出为:
Colors, Markers, and Line Styles


②刻度、标签和图例

  • 设置标题、轴标签、刻度和刻度标签
# 绘制随机漫步
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())

# 在数据范围内设置标签
ticks = ax.set_xticks([0, 250, 500, 750, 1000])
# 为设置好的标签赋值
labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
                            rotation=30, fontsize='small')      # rotation将X轴刻度标签旋转30度
# 设置标题
ax.set_title('My first matplotlib plot')
# 设置x轴名称
ax.set_xlabel('Stages')
  • 添加图例
from numpy.random import randn
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(randn(1000).cumsum(), 'k', label='one')         # 直线
ax.plot(randn(1000).cumsum(), 'k--', label='two')       # 虚线
ax.plot(randn(1000).cumsum(), 'k.', label='three')      # 散点
# 自动生成图例
ax.legend(loc='best')

输出为:
Setting the title, axis labels, ticks, and ticklabels


③注释与子图加工

from datetime import datetime

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

data = pd.read_csv('examples/spx.csv', index_col=0, parse_dates=True)
spx = data['SPX']

spx.plot(ax=ax, style='k-')

crisis_data = [
    (datetime(2007, 10, 11), 'Peak of bull market'),
    (datetime(2008, 3, 12), 'Bear Stearns Fails'),
    (datetime(2008, 9, 15), 'Lehman Bankruptcy')
]

for date, label in crisis_data:
    ax.annotate(label, xy=(date, spx.asof(date) + 75),
                xytext=(date, spx.asof(date) + 225),
                arrowprops=dict(facecolor='black', headwidth=4, width=2,
                                headlength=4),
                horizontalalignment='left', verticalalignment='top')

# Zoom in on 2007-2010
ax.set_xlim(['1/1/2007', '1/1/2011'])
ax.set_ylim([600, 1800])

ax.set_title('Important dates in the 2008-2009 financial crisis')

Annotations and Drawing on a Subplot


④将图片保存到文件(Saving Plots to File)

savefig()参数如下:

参数描述
fname包含文件路径或Python文件型对象的字符串。图片格式是从文件扩展名中推断出来的
dpi每英寸点数的分辨率,默认情况下是100,可以设置
facecolor,edgecolor子图之外的图形背景颜色,默认情况下是’w’(白色)
format文件格式(‘png’, ‘jpg’, ‘pdf’, ‘svg’, ‘ps’, ‘eps’…)
bbox_inches要保存的图片范围,如果传递’tight’,将会去除图片周围空白的部分
# plt.savefig('figpath.png', dpi=400, bbox_inches='tight')

# 不一定写入硬盘,可以将图片写入一切文件型对象中
from io import BytesIO
buffer = BytesIO()
# plt.savefig(buffer)
plot_data = buffer.getvalue()

2.使用pandas和seaborn绘图


①折线图(Line Plots)

Series.plot方法参数

参数描述
label图例标签
ax绘图所用的matplotlib子图对象;如果未传值,则使用当前的matplotlib子图
style传给matplotlib的样式字符串,比如 ko--
alpha图片不透明度
kind可以是’area’, ‘bar’, ‘barh’, ‘density’, ‘hist’, ‘kde’, ‘line’
logy在y轴上使用对数缩放
use_index使用对象索引刻度标签
rot刻度标签的旋转(0到360)
xticks用于x轴刻度的值
yticks用于y轴刻度的值
xlimsx轴的范围
ylimsy轴的范围
grid展示轴网络(默认打开)

DataFrame的plot参数

参数描述
subplots将DataFrame的每一列绘制在独立的字图中
sharex如果subplots=True,则共享相同的x轴、刻度和范围
sharey如果subplots=True,则共享相同的y轴、刻度和范围
figsize用于生成图片尺寸的元组
title标题字符串
legend添加子图图例
sort_columns按字母顺序绘制各列
s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
s.plot()        # 默认为折线图

df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
                  columns=['A', 'B', 'C', 'D'],
                  index=np.arange(0, 100, 10))
df.plot()   # 等价于: df.plot.line()

②柱状图

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)
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
df.plot.bar()

df.plot.barh(stacked=True, alpha=0.5)
# 统计一天内日参加派对的人数,并根据派对的规模算出日收益

tips = pd.read_csv('examples/tips.csv')
party_counts = pd.crosstab(tips['day'], tips['size'])
party_counts
# Not many 1- and 6-person parties
party_counts = party_counts.loc[:, 2:5]

# Normalize to sum to 1
party_pcts = party_counts.div(party_counts.sum(1), axis=0)
party_pcts
party_pcts.plot.bar()

plt.close('all')

import seaborn as sns
tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
print(tips.head(), '\n')
sns.barplot(x='tip_pct', y='day', data=tips, orient='h')

plt.close('all')

# 改变外观
sns.set(style="whitegrid")

4.散点图或点图

macro = pd.read_csv('examples/macrodata.csv')
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
trans_data = np.log(data).diff().dropna()
trans_data[-5:]

plt.figure()

# 绘制散点图并拟合出一条渐进回归线
sns.regplot('m1', 'unemp', data=trans_data)
plt.title('Changes in log %s versus log %s' % ('m1', 'unemp'))
# 设置画布参数,绘制回归/散点图
sns.pairplot(trans_data, diag_kind='kde', plot_kws={'alpha': 0.2})

在这里插入图片描述

在这里插入图片描述


5.分面网格和分类数据

  • sns.factorplot()
sns.factorplot(x='day', y='tip_pct', hue='time', col='smoker',
               kind='bar', data=tips[tips.tip_pct < 1])

在这里插入图片描述


6.其他Python可视化工具


九、pandas高级应用

  • 摘自《Python for Data Analysis(Second Edition)》

    • 详见【资源】- 案例分析.zip
  1. 分类数据
  2. Python建模库介绍
  3. 数据分析示例
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Larissa857

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值