matplotlib簡介
matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令A(yù)PI,十分適合交互式地行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應(yīng)用程序中。
它的文檔相當(dāng)完備,并且Gallery頁面中有上百幅縮略圖,打開之后都有源程序。因此如果你需要繪制某種類型的圖,只需要在這個頁面中瀏覽/復(fù)制/粘貼一下,基本上都能搞定。
在Linux下比較著名的數(shù)據(jù)圖工具還有g(shù)nuplot,這個是免費的,Python有一個包可以調(diào)用gnuplot,但是語法比較不習(xí)慣,而且畫圖質(zhì)量不高。
而 Matplotlib則比較強(qiáng):Matlab的語法、python語言、latex的畫圖質(zhì)量(還可以使用內(nèi)嵌的latex引擎繪制的數(shù)學(xué)公式)。
繪圖庫Matplotlib的安裝方法: 點擊這里
matplotlib繪制折線圖
1. line chart
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1) plt.plot(x, y2) plt.title('line chart') plt.xlabel('x') plt.ylabel('y') plt.show()
2. 圖例
在plot的時候指定label,然后調(diào)用legend方法可以繪制圖例。例如:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1, label='y = sin(x)') plt.plot(x, y2, label='y = cos(x)') plt.legend() plt.show()
legend方法可接受一個loc關(guān)鍵字參數(shù)來設(shè)定圖例的位置,可取值為數(shù)字或字符串:
???? 0: ‘best'
???? 1: ‘upper right'
???? 2: ‘upper left'
???? 3: ‘lower left'
???? 4: ‘lower right'
???? 5: ‘right'
???? 6: ‘center left'
???? 7: ‘center right'
???? 8: ‘lower center'
???? 9: ‘upper center'
???? 10: ‘center'
3. 線的樣式
(1)顏色
plot方法的關(guān)鍵字參數(shù)color(或c)用來設(shè)置線的顏色。可取值為:
1、顏色名稱或簡寫
???? b: blue
???? g: green
???? r: red
???? c: cyan
???? m: magenta
???? y: yellow
???? k: black
???? w: white
2、#rrggbb
3、(r, g, b) 或 (r, g, b, a),其中 r g b a 取均為[0, 1]之間
4、[0, 1]之間的浮點數(shù)的字符串形式,表示灰度值。0表示黑色,1表示白色
(2)樣式
plot方法的關(guān)鍵字參數(shù)linestyle(或ls)用來設(shè)置線的樣式。可取值為:
- -, solid
- --, dashed
- -., dashdot
- :, dotted
- '', ' ', None
(3)粗細(xì)
設(shè)置plot方法的關(guān)鍵字參數(shù)linewidth(或lw)可以改變線的粗細(xì),其值為浮點數(shù)。
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1, c='r', ls='--', lw=3) plt.plot(x, y2, c='#526922', ls='-.') plt.show()
4. marker
以下關(guān)鍵字參數(shù)可以用來設(shè)置marker的樣式:
- marker
- markeredgecolor 或 mec
- markeredgewidth 或 mew
- markerfacecolor 或 mfc
- markerfacecoloralt 或 mfcalt
- markersize 或 ms
其中marker可取值為:
- '.': point marker
- ',': pixel marker
- 'o': circle marker
- 'v': triangle_down marker
- '^': triangle_up marker
- '<': triangle_left marker
- '>': triangle_right marker
- '1': tri_down marker
- '2': tri_up marker
- '3': tri_left marker
- '4': tri_right marker
- 's': square marker
- 'p': pentagon marker
- '*': star marker
- 'h': hexagon1 marker
- 'H': hexagon2 marker
- '+': plus marker
- 'x': x marker
- 'D': diamond marker
- 'd': thin_diamond marker
- '|': vline marker
- '_': hline marker
例如:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 10) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1, marker='o', mec='r', mfc='w') plt.plot(x, y2, marker='*', ms=10) plt.show()
另外,marker關(guān)鍵字參數(shù)可以和color以及l(fā)inestyle這兩個關(guān)鍵字參數(shù)合并為一個字符串。例如:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 10) y1, y2 = np.sin(x), np.cos(x) plt.plot(x, y1, 'ro-') plt.plot(x, y2, 'g*:', ms=10) plt.show()
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
