matplotlib入门--1(条形图, 直方图, 盒须图, 饼图)
作图首先要进行数据的输入,matplotlib包只提供作图相关功能,本身并没有数据读入、输出函数,针对各种试验或统计文本数据输入可以使用numpy提供的数据输入函数。
# -*- coding: gbk -*- """ Created on Sun Jan 11 11:17:42 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] #生成数据 dataOut = np.arange(24).reshape(4, 6) print(dataOut) #保存数据 np.savetxt('data.txt', dataOut, fmt = '%.1f') #读取数据 data = np.loadtxt('data.txt') print(data)
plot 和 bar 函数
# -*- coding: gbk -*- """ Created on Sun Jan 11 11:33:14 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] data = np.random.randint(1, 11, 5) x = np.arange(len(data)) plt.plot(x, data, color = 'r') plt.bar(x, data, alpha = .5, color = 'g') plt.show()
结果图片
饼图
# -*- coding: gbk -*- """ Created on Sun Jan 11 11:33:14 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] data = np.random.randint(1, 11, 5) x = np.arange(len(data)) #plt.plot(x, data, color = 'r') #plt.bar(x, data, alpha = .5, color = 'g') plt.pie(data, explode = [0,0,.2, 0, 0]) plt.show
在实际工作中经常要对多组数据进行对比分析,这样需要在一个图表里表示出多个数据集。plot函数多数据集表示方法:
# -*- coding: gbk -*- """ Created on Sun Jan 11 11:51:41 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] data = np.random.randint(1, 5, (5, 2)) x = np.arange(len(data)) plt.plot(x, data[:, 0], '--', color = 'm') plt.plot(x, data[:, 1], '-.', color = 'c') plt.show()
这里用到了matplotlib中defered rendering的概念,它是指在绘图过程中,只有你调用到plt.plot函数是其它的绘图指令才会起效。
也可以通过对条形图的定制实现数据对比,主要有这几种类型 multy bar chart;stack bar chart和back to back bar chart
# -*- coding: gbk -*- """ Created on Sun Jan 11 12:03:57 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False data = np.random.randint(1, 5, [3, 4]) index = np.arange(data.shape[1]) color_index = ['r', 'g', 'b'] fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize = (5, 12)) for i in range(data.shape[0]): ax1.bar(index + i*.25 + .1, data[i], width = .25, color = color_index[i],\ alpha = .5) for i in range(data.shape[0]): ax2.bar(index + .25, data[i], width = .5, color = color_index[i],\ bottom = np.sum(data[:i], axis = 0), alpha = .7) ax3.barh(index, data[0], color = 'r', alpha = .5) ax3.barh(index, -data[1], color = 'b', alpha = .5) plt.show() plt.savefig('complex_bar_chart')
统计中常用的两种图标是直方图和盒须图,matplotlib中有针对这两种图表的专门函数:hist和boxplot
# -*- coding: gbk -*- """ Created on Sun Jan 11 12:29:34 2015 @author: zhang """ import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['font.family'] = 'sans-serif' mpl.rcParams['font.sans-serif'] = [u'SimHei'] data = np.random.randn(100) fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (8, 4)) ax1.hist(data) ax2.boxplot(data) plt.savefig('hist_boxplot') plt.show()
本文讲到的所有matplotlib命令都有非常丰富的定制参数,我会在后面文章中讲到,你也可以查看帮助文档学习。