Python Matplotlib庫入門指南

來源:互聯網
上載者:User
Matplotlib簡介

Matplotlib是一個Python工具箱,用於科學計算的資料視覺效果。藉助它,Python可以繪製如Matlab和Octave多種多樣的資料圖形。最初是模仿了Matlab圖形命令, 但是與Matlab是相互獨立的.
通過Matplotlib中簡單的介面可以快速的繪製2D圖表

初試Matplotlib

Matplotlib中的pyplot子庫提供了和matlab類似的繪圖API.

代碼如下:


import matplotlib.pyplot as plt #匯入pyplot子庫
plt.figure(figsize=(8, 4)) #建立一個繪圖對象, 並設定對象的寬度和高度, 如果不建立直接調用plot, Matplotlib會直接建立一個繪圖對象
plt.plot([1, 2, 3, 4]) #此處設定y的座標為[1, 2, 3, 4], 則x的座標預設為[0, 1, 2, 3]在繪圖對象中進行繪圖, 可以設定label, color和linewidth關鍵字參數
plt.ylabel('some numbers') #給y軸添加標籤, 給x軸加標籤用xlable
plt.title("hello"); #給2D圖加標題
plt.show() #顯示2D圖

基礎繪圖

繪製折線圖

與所選點的座標有關

代碼如下:


# -*- 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.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()


更改線的樣式查看plot函數參數設定
多線圖
只需要在plot函數中傳入多對x-y座標組就能畫出多條線

代碼如下:


# -*- 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()函數指明numrows行數, numcols列數, fignum圖個數. 圖的個數不能超過行數和列數之積

代碼如下:


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

文本添加

當需要在圖片上調價文本時需要使用text()函數, 還有xlabel(), ylabel(), title()函數

text()函數返回matplotlib.text.Text, 函數詳細解釋

代碼如下:


# -*- 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") //前兩個參數表示文本座標, 第三個參數為要添加的文本
plt.show()

圖例簡介
legend()函數實現了圖例功能, 他有兩個參數, 第一個為樣式對象, 第二個為描述字元

代碼如下:


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


或者調用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()


更多圖例設定可以參考官方圖例教程
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.