Python Matplotlib庫入門指南
Matplotlib簡介
Matplotlib是一個Python工具箱,用于科學(xué)計算的數(shù)據(jù)可視化。借助它,Python可以繪制如Matlab和Octave多種多樣的數(shù)據(jù)圖形。最初是模仿了Matlab圖形命令, 但是與Matlab是相互獨立的.
通過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ǔ)繪圖
繪制折線圖
與所選點的坐標(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') # 虛線, 星點, 紅色
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ù)實現(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è)置可以參考官方圖例教程
相關(guān)文章
巧妙使用Python裝飾器處理if...elif...else
大家好,今天在 Github 閱讀 EdgeDB[1] 的代碼,發(fā)現(xiàn)它在處理大量if…elif…else的時候,巧妙地使用了裝飾器,方法設(shè)計精巧,分享給大家一下,歡迎收藏學(xué)習(xí),喜歡點贊支持2021-11-11
用Pelican搭建一個極簡靜態(tài)博客系統(tǒng)過程解析
這篇文章主要介紹了用Pelican搭建一個極簡靜態(tài)博客系統(tǒng)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
pytest實戰(zhàn)技巧之參數(shù)化基本用法和多種方式
本文介紹了pytest參數(shù)化的基本用法和多種方式,幫助讀者更好地使用這個功能,同時,還介紹了一些高級技巧,如動態(tài)生成參數(shù)名稱、參數(shù)化的組合和動態(tài)生成參數(shù)化裝飾器,幫助讀者更靈活地使用參數(shù)化,感興趣的朋友參考下吧2023-12-12

