作圖首先要進(jìn)行數(shù)據(jù)的輸入,matplotlib包只提供作圖相關(guān)功能,本身并沒(méi)有數(shù)據(jù)讀入、輸出函數(shù),針對(duì)各種試驗(yàn)或統(tǒng)計(jì)文本數(shù)據(jù)輸入可以使用numpy提供的數(shù)據(jù)輸入函數(shù)。
# -*- coding: gbk -*-"""Created on Sun Jan 11 11:17:42 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rcParams['font.family'] = 'sans-serif'mpl.rcParams['font.sans-serif'] = [u'SimHei']#生成數(shù)據(jù)dataOut = np.arange(24).reshape(4, 6)PRint(dataOut)#保存數(shù)據(jù)np.savetxt('data.txt', dataOut, fmt = '%.1f')#讀取數(shù)據(jù)data = np.loadtxt('data.txt')print(data)
plot 和 bar 函數(shù)
# -*- coding: gbk -*-"""Created on Sun Jan 11 11:33:14 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.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()
結(jié)果圖片
餅圖
# -*- coding: gbk -*-"""Created on Sun Jan 11 11:33:14 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.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
在實(shí)際工作中經(jīng)常要對(duì)多組數(shù)據(jù)進(jìn)行對(duì)比分析,這樣需要在一個(gè)圖表里表示出多個(gè)數(shù)據(jù)集。plot函數(shù)多數(shù)據(jù)集表示方法:
# -*- coding: gbk -*-"""Created on Sun Jan 11 11:51:41 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.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的概念,它是指在繪圖過(guò)程中,只有你調(diào)用到plt.plot函數(shù)是其它的繪圖指令才會(huì)起效。
也可以通過(guò)對(duì)條形圖的定制實(shí)現(xiàn)數(shù)據(jù)對(duì)比,主要有這幾種類(lèi)型 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 npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rcParams['font.family'] = 'sans-serif'mpl.rcParams['font.sans-serif'] = [u'SimHei']mpl.rcParams['axes.unicode_minus'] = Falsedata = 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')
統(tǒng)計(jì)中常用的兩種圖標(biāo)是直方圖和盒須圖,matplotlib中有針對(duì)這兩種圖表的專(zhuān)門(mén)函數(shù):hist和boxplot
# -*- coding: gbk -*-"""Created on Sun Jan 11 12:29:34 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.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命令都有非常豐富的定制參數(shù),我會(huì)在后面文章中講到,你也可以查看幫助文檔學(xué)習(xí)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注