博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绘图和可视化(matplotlib)
阅读量:4068 次
发布时间:2019-05-25

本文共 3335 字,大约阅读时间需要 11 分钟。

《Python for Data Analysis》

绘图和可视化是数据分析中的一项重要工作。通过可视化,能够更好的观察数据的模式,帮助我们找出数据的异常值、必要的数据转换、得出有关模型的想法。

matplotlib

用法:

  • 在ipython中,使用ipython --pylab模式启动;

  • 或jupyter notebook中,%matplotlib inline (better!)

In [1]: import numpy as np   ...: data = np.arange(10)   ...: data   ...: plt.plot(data)   ...:Out[1]: [
]

这里写图片描述

Figure对象

In [3]: fig = plt.figure()In [4]: ax1 = fig.add_subplot(2, 2, 1)In [5]: ax2 = fig.add_subplot(2, 2, 2)   ...: ax3 = fig.add_subplot(2, 2, 3)   ...:In [6]: plt.plot(np.random.randn(50).cumsum(), 'k--')Out[6]: [
]

这里写图片描述

subplots方法

创建一个新的Figure对象,并返回一个含有已创建的subplot对象的Numpy数组。

In [7]: fig, axes = plt.subplots(2, 3)   ...: axes   ...:Out[7]:array([[
,
,
], [
,
,
]], dtype=object)

这里写图片描述

参数 选项
nrows subplot的行数
ncols subplot的列数
sharex 所有subplot使用相同的X轴刻度(调节xlim会影响所有subplot)
sharey 共享Y轴刻度
subplot_kw 用于创建各subplot的关键字字典
**fig_kw 创建figure的其他关键字,如plot.subplots(2,2,figuresize=(8,6))

调整subplot周围的间距:subplots_adjust方法

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

In [8]: fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)   ...: for i in range(2):   ...:     for j in range(2):   ...:         axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)   ...: plt.subplots_adjust(wspace=0, hspace=0)   ...:

这里写图片描述

颜色、标记和线型

In [9]: plt.figure()   ...: from numpy.random import randn   ...: plt.plot(randn(30).cumsum(), 'ko--')   ...:Out[9]: [
]

这里写图片描述

In [10]: 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')    ...:Out[10]: 

这里写图片描述

刻度、标签和图例

In [11]: 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')    ...: ax.set_title('My first matplotlib plot')    ...: ax.set_xlabel('Stages')    ...:Out[11]: 

这里写图片描述

In [12]: 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')    ...:Out[12]: 

这里写图片描述

注解以及在Subplot上绘图

from datetime import datetimefig = 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-2010ax.set_xlim(['1/1/2007', '1/1/2011'])ax.set_ylim([600, 1800])ax.set_title('Important dates in the 2008-2009 financial crisis')

这里写图片描述

你可能感兴趣的文章
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>