在进行数据分析时,绘图是必不可少的模式探索方式。用Python进行数据分析时,matplotlib和pandas是最常用到的两个库。
1、matplotlib库的应用
准备工作如下:打开ipython,输入命令分别导入numpy和matplotlib.pylab库。
importnumpy as np
importmatplotlib.pylab as plt
1)创建fig
绘图第一步是创建绘图窗口fig。
fig1 = plt.figure()
2)创建subplot
在窗口上添加AxesSubplot类型的子绘图区域,一个窗口可以添加多个子绘图区。
ax1 = fig1.add_subplot(2,2,1)
ax4 = fig1.add_subplot(2,2,4)
3)subplot中绘图
调用子绘图区的方法,可以绘制点线图、频数图、散点图等常用图形。
注意:在同一个subplot中多次调用plot(),所得到的图形是相互覆盖的。
ax1.plot(np.random.randn(50).cumsum(),'k--')
ax4.hist(np.random.randn(30))
4)各类参数设置
主要关注以下几种方法:set_xlims设置坐标轴的上下限、set_ticks设置坐标刻度、set_ticklabel设置坐标标注。
ax1.set_xlim(-10,60)
ax1.set_xticks([0,20,40,60])
ax1.set_xticklabels(['a','b','c','d