Python編程深度學(xué)習(xí)繪圖庫(kù)之matplotlib
matplotlib是python的一個(gè)開源的2D繪圖庫(kù),它的原作者是John D. Hunter,因?yàn)樵谠O(shè)計(jì)上借鑒了matlab,所以成為matplotlib。和Pillow一樣是被廣泛使用的繪圖功能,而在深度學(xué)習(xí)相關(guān)的部分,matplotlib得寵的多。這篇文章將簡(jiǎn)單介紹一下如何安裝以及使用它來(lái)畫一些非常常見的統(tǒng)計(jì)圖形。
概要信息
注意事項(xiàng):由于Python2支持到2020年,很多python庫(kù)都開始主要支持python3了,matplotlib的主分支也已經(jīng)是python3了。而這篇文章中為了簡(jiǎn)單,安裝和demo代碼依然是在python2.7上進(jìn)行驗(yàn)證的。
安裝
使用pip install即可直接安裝。安裝日志如下:
liumiaocn:tmp liumiao$ python -mpip install -U pip Requirement already up-to-date: pip in /usr/local/lib/python2.7/site-packages (10.0.1) liumiaocn:tmp liumiao$ liumiaocn:tmp liumiao$ python -mpip install -U matplotlib Collecting matplotlib Downloading https://files.pythonhosted.org/packages/61/38/d70e8bf77d5cb27d5f3595edd0b3978825063feadd023786d2591e393e6e/matplotlib-2.2.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (13.7MB) 100% |████████████████████████████████| 13.7MB 2.1MB/s Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib) Downloading https://files.pythonhosted.org/packages/6a/8a/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b026a50/pyparsing-2.2.0-py2.py3-none-any.whl (56kB) 100% |████████████████████████████████| 61kB 5.8MB/s Collecting backports.functools-lru-cache (from matplotlib) Downloading https://files.pythonhosted.org/packages/03/8e/2424c0e65c4a066e28f539364deee49b6451f8fcd4f718fefa50cc3dcf48/backports.functools_lru_cache-1.5-py2.py3-none-any.whl Collecting subprocess32 (from matplotlib) Downloading https://files.pythonhosted.org/packages/fa/60/b50459f291cae6bc1d0ff711b75e5130684fd3949370fdba78f6c57c1903/subprocess32-3.5.2-cp27-cp27m-macosx_10_6_intel.whl Collecting pytz (from matplotlib) Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB) 100% |████████████████████████████████| 512kB 8.9MB/s Requirement not upgraded as not directly required: six>=1.10 in /usr/local/lib/python2.7/site-packages (from matplotlib) (1.11.0) Collecting python-dateutil>=2.1 (from matplotlib) Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB) 100% |████████████████████████████████| 215kB 7.2MB/s Collecting kiwisolver>=1.0.1 (from matplotlib) Downloading https://files.pythonhosted.org/packages/79/d8/94633718f3f77dcb638687a77ba199325a1cb158d2d4b00c9dc17f2b5c72/kiwisolver-1.0.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (110kB) 100% |████████████████████████████████| 112kB 5.9MB/s Collecting cycler>=0.10 (from matplotlib) Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl Requirement not upgraded as not directly required: numpy>=1.7.1 in /usr/local/lib/python2.7/site-packages (from matplotlib) (1.14.5) Requirement not upgraded as not directly required: setuptools in /usr/local/lib/python2.7/site-packages (from kiwisolver>=1.0.1->matplotlib) (39.2.0) Installing collected packages: pyparsing, backports.functools-lru-cache, subprocess32, pytz, python-dateutil, kiwisolver, cycler, matplotlib Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0 python-dateutil-2.7.3 pytz-2018.4 subprocess32-3.5.2 liumiaocn:tmp liumiao$
確認(rèn)
liumiaocn:~ liumiao$ pip show matplotlib Name: matplotlib Version: 2.2.2 Summary: Python plotting package Home-page: http://matplotlib.org Author: John D. Hunter, Michael Droettboom Author-email: matplotlib-users@python.org License: BSD Location: /usr/local/lib/python2.7/site-packages Requires: pyparsing, backports.functools-lru-cache, subprocess32, pytz, six, python-dateutil, kiwisolver, cycler, numpy Required-by: liumiaocn:~ liumiao$
使用
正弦函數(shù)
我們使用numpy和matplotlib可以非常簡(jiǎn)單地就能夠畫出正弦的波形圖,具體代碼如下:
liumiaocn:plot liumiao$ cat plot-1.py #!/usr/local/bin/python import numpy as np import matplotlib.pyplot as plot print("y=sin(x): x with range fo -4pi to 4pi") x=np.arange(-4*np.pi, 4*np.pi, 0.1) y=np.sin(x) #print sin plot.plot(x,y) plot.show() liumiaocn:plot liumiao$
執(zhí)行&結(jié)果確認(rèn)
liumiaocn:plot liumiao$ python plot-1.py y=sin(x): x with range fo -4pi to 4pi
直方圖
最簡(jiǎn)單的直方圖的做法,可以通過(guò)bar或者h(yuǎn)ist來(lái)創(chuàng)建
liumiaocn:plot liumiao$ cat plot-2.py import numpy as np import matplotlib.pyplot as plt x = np.arange(7) y = (10,15,24,7,14,30,20) plt.bar(x,y) plt.show() liumiaocn:plot liumiao$
餅圖
使用pie函數(shù)創(chuàng)建餅圖
liumiaocn:plot liumiao$ cat plot-3.py #!/usr/local/bin/python import matplotlib.pyplot as plt dateinf = '06/01', '06/02', '06/03', '06/04', '06/05', '06/06', '06/07' bugnums = [11,22,7,29,24,15,18] plt.pie(bugnums, labels=dateinf) plt.show() liumiaocn:plot liumiao$
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Python如何獲取免費(fèi)高匿代理IP及驗(yàn)證
這篇文章主要介紹了Python如何獲取免費(fèi)高匿代理IP及驗(yàn)證問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06keras獲得某一層或者某層權(quán)重的輸出實(shí)例
今天小編就為大家分享一篇keras獲得某一層或者某層權(quán)重的輸出實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python 實(shí)現(xiàn)向word(docx)中輸出
今天小編就為大家分享一篇Python 實(shí)現(xiàn)向word(docx)中輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)
這篇文章主要介紹了Python 改變數(shù)組類型為uint8的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python fabric實(shí)現(xiàn)遠(yuǎn)程操作和部署示例
這篇文章主要介紹了python使用fabric實(shí)現(xiàn)遠(yuǎn)程操作和部署示例,需要的朋友可以參考下2014-03-03Python?八個(gè)數(shù)據(jù)清洗實(shí)例代碼詳解
不管你承不承認(rèn),數(shù)據(jù)清洗著實(shí)不是一件簡(jiǎn)單的任務(wù),大多數(shù)情況下這項(xiàng)工作是十分耗時(shí)而乏味的,但它又是十分重要的,本篇文章帶給你八個(gè)實(shí)例代碼2022-01-01python中使用urllib2偽造HTTP報(bào)頭的2個(gè)方法
這篇文章主要介紹了python中使用urllib2偽造HTTP報(bào)頭的2個(gè)方法,即偽造http頭信息,需要的朋友可以參考下2014-07-07