Matplotlib簡介
Matplotlib是一個Python工具箱,用于科學(xué)計算的數(shù)據(jù)可視化。借助它,Python可以繪制如Matlab和Octave多種多樣的數(shù)據(jù)圖形。最初是模仿了Matlab圖形命令, 但是與Matlab是相互獨(dú)立的.
通過Matplotlib中簡單的接口可以快速的繪制2D圖表
初試Matplotlib
Matplotlib中的pyplot子庫提供了和matlab類似的繪圖API.
import matplotlib.pyplot as plt?? #導(dǎo)入pyplot子庫
plt.figure(figsize=(8, 4))? #創(chuàng)建一個繪圖對象, 并設(shè)置對象的寬度和高度, 如果不創(chuàng)建直接調(diào)用plot, Matplotlib會直接創(chuàng)建一個繪圖對象
plt.plot([1, 2, 3, 4])? #此處設(shè)置y的坐標(biāo)為[1, 2, 3, 4], 則x的坐標(biāo)默認(rèn)為[0, 1, 2, 3]在繪圖對象中進(jìn)行繪圖, 可以設(shè)置label, color和linewidth關(guān)鍵字參數(shù)
plt.ylabel('some numbers')? #給y軸添加標(biāo)簽, 給x軸加標(biāo)簽用xlable
plt.title("hello");? #給2D圖加標(biāo)題
plt.show()? #顯示2D圖
基礎(chǔ)繪圖
繪制折線圖
與所選點(diǎn)的坐標(biāo)有關(guān)
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 5, 6]
y = [1, 2, 3, 2, 4, 1]
plt.plot(x, y, '-*r')? # 虛線, 星點(diǎn), 紅色
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
更改線的樣式查看 plot函數(shù)參數(shù)設(shè)置?
多線圖
只需要在plot函數(shù)中傳入多對x-y坐標(biāo)對就能畫出多條線
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 5, 6]
y = [1, 2, 3, 2, 4, 1]
z = [1, 2, 3, 4, 5, 6]
plt.plot(x, y, '--*r', x, z, '-.+g')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("hello world")
plt.show()
柱狀圖
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 5, 6]
y = [1, 2, 3, 2, 4, 1]
z = [1, 2, 3, 4, 5, 6]
plt.bar(x, y)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
子圖
subplot()函數(shù)指明numrows行數(shù), numcols列數(shù), fignum圖個數(shù). 圖的個數(shù)不能超過行數(shù)和列數(shù)之積
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 5, 6]
y = [1, 2, 3, 2, 4, 1]
z = [1, 2, 3, 4, 5, 6]
plt.figure(1)
plt.subplot(211)
plt.plot(x, y, '-+b')
plt.subplot(212)
plt.plot(x, z, '-.*r')
plt.show()
文本添加
當(dāng)需要在圖片上調(diào)價文本時需要使用text()函數(shù), 還有xlabel(), ylabel(), title()函數(shù)
text()函數(shù)返回matplotlib.text.Text, 函數(shù)詳細(xì)解釋
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 5, 6]
y = [1, 2, 3, 2, 4, 1]
plt.plot(x, y, '-.*r')
plt.text(1, 2, "I'm a text")? //前兩個參數(shù)表示文本坐標(biāo), 第三個參數(shù)為要添加的文本
plt.show()
圖例簡介
legend()函數(shù)實(shí)現(xiàn)了圖例功能, 他有兩個參數(shù), 第一個為樣式對象, 第二個為描述字符
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend(handles=[line_up, line_down])
plt.show()
或者調(diào)用set_label()添加圖例
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
line, = plt.plot([1, 2, 3])
line.set_label("Label via method")
plt.legend()
plt.show()
同時對多條先添加圖例
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
line1, = plt.plot([1, 2, 3])
line2, = plt.plot([3, 2, 1], '--b')
plt.legend((line1, line2), ('line1', 'line2'))
plt.show()
更多圖例設(shè)置可以參考 官方圖例教程
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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