国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

matplotlib入門(mén)--1(條形圖,直方圖,盒須圖,餅圖)

2019-11-14 17:25:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

作圖首先要進(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é)果圖片

image

餅圖

# -*- 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

image

在實(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()

image

這里用到了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')

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()

hist_boxplot

本文講到的所有matplotlib命令都有非常豐富的定制參數(shù),我會(huì)在后面文章中講到,你也可以查看幫助文檔學(xué)習(xí)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沛县| 高州市| 岢岚县| 南乐县| 文化| 灵璧县| 志丹县| 彭山县| 黄山市| 确山县| 万安县| 白城市| 治县。| 始兴县| 姜堰市| 库车县| 日土县| 黎城县| 廊坊市| 宁晋县| 弥渡县| 峡江县| 龙山县| 台中市| 伊金霍洛旗| 濮阳县| 昂仁县| 大埔区| 游戏| 叶城县| 兴国县| 河池市| 南安市| 米泉市| 五大连池市| 西宁市| 久治县| 阿尔山市| 长丰县| 安乡县| 自治县|