Python Matplotlib庫入門指南
Matplotlib簡介
Matplotlib是一個Python工具箱,用于科學計算的數(shù)據(jù)可視化。借助它,Python可以繪制如Matlab和Octave多種多樣的數(shù)據(jù)圖形。最初是模仿了Matlab圖形命令, 但是與Matlab是相互獨立的.
通過Matplotlib中簡單的接口可以快速的繪制2D圖表
初試Matplotlib
Matplotlib中的pyplot子庫提供了和matlab類似的繪圖API.
import matplotlib.pyplot as plt #導入pyplot子庫
plt.figure(figsize=(8, 4)) #創(chuàng)建一個繪圖對象, 并設置對象的寬度和高度, 如果不創(chuàng)建直接調(diào)用plot, Matplotlib會直接創(chuàng)建一個繪圖對象
plt.plot([1, 2, 3, 4]) #此處設置y的坐標為[1, 2, 3, 4], 則x的坐標默認為[0, 1, 2, 3]在繪圖對象中進行繪圖, 可以設置label, color和linewidth關鍵字參數(shù)
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函數(shù)參數(shù)設置
多線圖
只需要在plot函數(shù)中傳入多對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()函數(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()
文本添加
當需要在圖片上調(diào)價文本時需要使用text()函數(shù), 還有xlabel(), ylabel(), title()函數(shù)
text()函數(shù)返回matplotlib.text.Text, 函數(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]
plt.plot(x, y, '-.*r')
plt.text(1, 2, "I'm a text") //前兩個參數(shù)表示文本坐標, 第三個參數(shù)為要添加的文本
plt.show()
圖例簡介
legend()函數(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()
更多圖例設置可以參考官方圖例教程
相關文章
如何用Python對數(shù)學函數(shù)進行求值、求偏導
這篇文章主要介紹了如何用Python對數(shù)學函數(shù)進行求值、求偏導問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Python實戰(zhàn)快速上手BeautifulSoup庫爬取專欄標題和地址
BeautifulSoup是爬蟲必學的技能,BeautifulSoup最主要的功能是從網(wǎng)頁抓取數(shù)據(jù),Beautiful Soup自動將輸入文檔轉換為Unicode編碼,輸出文檔轉換為utf-8編碼2021-10-10CentOS 7下Python 2.7升級至Python3.6.1的實戰(zhàn)教程
Centos是目前最為流行的Linux服務器系統(tǒng),其默認的Python 2.x,這篇文章主要給大家分享了關于在CentOS 7下Python 2.7升級至Python3.6.1的實戰(zhàn)教程,文中將升級的步驟一步步的介紹的非常詳細,對大家的理解和學習具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07python?OpenCV實現(xiàn)圖像特征匹配示例詳解
這篇文章主要為大家介紹了python?OpenCV實現(xiàn)圖像特征匹配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04使用Python實現(xiàn)一個簡單的項目監(jiān)控
這篇文章主要介紹了使用Python實現(xiàn)一個簡單的項目監(jiān)控,包括連接數(shù)據(jù)庫進行查詢等操作,需要的朋友可以參考下2015-03-03