Python Matplotlib初階使用入門教程
0. 前言
本文介紹Python Matplotlib庫的入門求生級使用方法。
為了方便以下舉例說明,我們先導(dǎo)入需要的幾個庫。以下代碼在Jupyter Notebook中運行。
%matplotlib inline import matplotlib.pyplot as plt import numpy as np import pandas as pd
1. 創(chuàng)建Figure的兩種基本方法
1.1 第1種方法
注意,F(xiàn)igure在Matplotlib中是一個專有名詞(后面會有解釋),Matplotlib把你的數(shù)據(jù)以圖的形式繪制在Figure(比如說,windows, Jupyter wgets, etc.)中。創(chuàng)建一個Figure的基本方式之一就是使用pyplot.subplots.
如下所示,我們用pyplot.subplots創(chuàng)建了一個Figure,其中包含一個axes(同樣,這是一個Matplotlib專有名詞,后面再進行解釋),然后再利用axes.plot畫圖。
fig, ax = plt.subplots() # Create a figure containing a single axes. x = np.arange(100) Fs = 100 # 100Hz sampling rate Fsin = 2 # 2Hz y = np.sin(2*np.pi*Fsin*(1/Fs)*x) ax.plot(x, y) # Plot some data on the axes.
以上代碼畫了一個正弦波的波形。

1.2 第2種方法
許多其它的繪圖工具庫或者語言并不要求你必須顯式地(explicity)先創(chuàng)建一個Figure以及axes,比如說在Matlab中,你直接如下所示一樣直接調(diào)用plot()函數(shù)就一步到位地得到所想要的圖,如果現(xiàn)成的axes的話,matlab會為你創(chuàng)建一個.
plot([1, 2, 3, 4], [1, 4, 2, 3]) % MATLAB plot.
事實上,你也可以以這種方式使用Matplotlib。對于每一個axes的繪圖方法,matplotlib.pyplot模塊中都有一個對應(yīng)的函數(shù)執(zhí)行在當(dāng)前axes上畫一個圖的功能,如果還沒有已經(jīng)創(chuàng)建好的axes的話,就先為你創(chuàng)建一個Figure和axes,正如在Matlab中一樣。因此以上例子可以更簡潔地寫為:
plt.plot(x, y) # Call plt.plot() directly
同樣會得到以上相同的圖。
2. Figure的解剖圖及各種基本概念

上圖出自Ref1。圖中給出了matplotlib中Figure(沒有想好這個到底應(yīng)該怎么翻譯。本來想是不是可以譯成畫布,但是畫布對應(yīng)英文中的Canvas。而Figure的說明中明確指出了Figure包含了Canvas,如果說Canvas是指畫布的話,那Figure其實是指整個一幅畫。還是不勉強吧,直接用英語單詞好了)
2.1 Figure
The whole figure.整幅畫或整個圖。Figure保持其中所有的child axes的信息,以及一些特殊的artists (titles, figure legends, etc)(這里artist又是很難翻譯的一個詞,指的是標(biāo)題、圖例等圖的說明性信息對象),以及畫布(canvas)。
可以說Figure是幕后的大Boss管理著所有的信息,是它真正執(zhí)行為你繪制圖畫的動作。但是作為用戶,它反而是不太顯眼的,對你來說多少有點隱形的樣子。一個Figure中可以包含任意多個axes,通常至少包含一個。
以下是創(chuàng)建Figure的幾種方式,其中前兩種在上面的例子已經(jīng)介紹,也是常用的。
最后一種是創(chuàng)建一個空的Figure,可以在之后給它添加axes。這種做法不常見,屬于高階用法,方便高階用戶進行更加靈活的axes布局。
fig, ax = plt.subplots() # a figure with a single Axes fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes fig = plt.figure() # an empty figure with no Axes
2.2 Axes
Axes原本是axis(坐標(biāo))的復(fù)數(shù),所以可以翻譯成坐標(biāo)系?就這么理解著吧。一個給定的Figure可以包含多個axes,但是一個axes對象只能存在于一個Figure中。(按坐標(biāo)系理解的話,故名思意)一個Axes包含多個axis,比如說在2D(2維)的圖中有兩個坐標(biāo)軸,在3D(3維)圖中有三個坐標(biāo)軸。
在一個axes中可以通過以下一些方法來設(shè)置圖的一些屬性:
set_xlim(),set_ylim():分別用于設(shè)置x軸、y軸的表示范圍
set_title():設(shè)置圖的標(biāo)題
set_xlabel(),set_ylabel(): 分別用于設(shè)置x軸和y軸的標(biāo)簽
Axes類和它的成員函數(shù)是matplolib的面向?qū)ο蟮慕缑娴闹饕肟冢P(guān)于面向?qū)ο蠼涌谂cpyplot接口參見后面的說明)
2.3 Axis
這個就是我們常說的坐標(biāo)軸。不再贅述。事實上,越解釋越糊涂。。。有興趣的小伙伴可以看看matplotlib文檔的原始說明,反正我是越看越暈。。。還不如直接看代碼例直到怎么使用就可以了。
2.4 Artist
Basically, everything you can see on the figure is an artist (even the Figure, Axes, and Axis objects). This includes Text objects, Line2D objects, collections objects, Patch objects ... (you get the ea). When the figure is rendered, all of the artists are drawn to the canvas. Most Artists are tied to an Axes; such an Artist cannot be shared by multiple Axes, or moved from one to another.
暈菜。。。同上,還不如直接看代碼例直到怎么使用就可以了^-^
3. 繪圖函數(shù)的輸入
所有的繪圖函數(shù)都接受numpy.array or numpy.ma.masked_array作為輸入。
像pandas中的數(shù)據(jù)對象以及numpy.matrix等類似于數(shù)組('array-like')的對象如果直接用于繪圖函數(shù)的輸入的話有可能會產(chǎn)生意想不到的結(jié)果。所以最好把它們先轉(zhuǎn)換成numpy.array對象再傳遞給繪圖函數(shù)。
# For example, to convert a pandas.DataFrame
a = pd.DataFrame(np.random.rand(4, 5), columns = list('abcde'))
a_asarray = a.values
# and to convert a numpy.matrix
b = np.matrix([[1, 2], [3, 4]])
b_asarray = np.asarray(b)
4. 面向?qū)ο蠼涌谂cpyplot接口
如上所述,有兩種基本的使用Matplotlib的方法。
(1) 顯式地創(chuàng)建Figure和axes,然后調(diào)用方法作用于它們,這個稱之為面向?qū)ο箫L(fēng)格。
(2) 直接調(diào)用pyplot進行繪圖,這個姑且稱之為快捷風(fēng)格吧
面向?qū)ο箫L(fēng)格的使用方法示例:
x = np.linspace(0, 2, 100)
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
fig, ax = plt.subplots() # Create a figure and an axes.
ax.plot(x, x, label='linear') # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic') # Plot more data on the axes...
ax.plot(x, x**3, label='cubic') # ... and some more.
ax.set_xlabel('x label') # Add an x-label to the axes.
ax.set_ylabel('y label') # Add a y-label to the axes.
ax.set_title("Simple Plot") # Add a title to the axes.
ax.legend() # Add a legend.
快捷(pyplot)風(fēng)格的使用方法示例:
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear') # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic') # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
以上代碼中同時給出了兩種風(fēng)格中的label、title、legend等設(shè)置的方法或函數(shù)使用例。
以上兩段代碼示例產(chǎn)生同樣的繪圖效果:

此外,還有第三種方法,用于在GUI應(yīng)用中的嵌入式Matplotlib。這個屬于進階用法,在本文就不做介紹了。
面向?qū)ο箫L(fēng)格和pyplot風(fēng)格功能相同同樣好用,你可以選擇使用任何一種風(fēng)格。但是最好選定其中一種使用,不要昏庸。一般來說,建議僅在交互式繪圖(比如說在Jupyter notebook)中使用pyplot風(fēng)格,而在面向非交互式繪圖中則推薦使用面向?qū)ο箫L(fēng)格。
注意,在一些比較老的代碼例中,你還可以看到使用pylab接口。但是現(xiàn)在不建議這樣用了(This approach is strongly discouraged nowadays and deprecated)。這里提一嘴只是因為偶爾你還可能看到它,但是,在新的代碼不要用就好了。
5. 繪圖復(fù)用實用函數(shù)例
通常我們會發(fā)現(xiàn)需要重復(fù)繪制很多相類似的圖,只是采用不同的數(shù)據(jù)而已。為了提高效率減少錯誤,可以考慮將一些繪圖處理封裝成一個函數(shù),以便于重復(fù)使用,避免過多的冗余代碼。以下是一個這樣的模板例子:
def my_plotter(ax, data1, data2, param_dict):
"""
A helper function to make a graph
Parameters
----------
ax : Axes
The axes to draw to
data1 : array
The x data
data2 : array
The y data
param_dict : dict
Dictionary of kwargs to pass to ax.plot
Returns
-------
out : list
list of artists added
"""
out = ax.plot(data1, data2, **param_dict)
return out
然后你可以以如下方式使用:
x = np.linspace(0, 5, 20)
fig, ax = plt.subplots()
x2 = x**2
x3 = x**3
my_plotter(ax, x, x2, {'marker': 'o'})
my_plotter(ax, x, x3, {'marker': 'd'})

或者,如果你需要多個子圖的話,
x = np.linspace(0, 5, 20)
x2 = x**2
x3 = x**3
fig, (ax1, ax2) = plt.subplots(1, 2)
my_plotter(ax1, x, x2, {'marker': 'x'})
my_plotter(ax2, x, x3, {'marker': 'o'})
fig, ax = plt.subplots(1, 2)
my_plotter(ax[0], x, x2, {'marker': 'x'})
my_plotter(ax[1], x, x3, {'marker': 'o'})

注意,如以上代碼例所示,當(dāng)創(chuàng)建了多個子圖時,有兩種引用axes的方式。第一種方式中,創(chuàng)建時直接將兩個axes(每個子圖對應(yīng)一個axes)賦給ax1和ax2。第一種方式中,創(chuàng)建時直接將兩個axes賦給一個axes數(shù)組ax,然后以ax[0]和ax[1]的格式進行引用。
Ref1: Usage Gue — Matplotlib 3.4.3 documentation
到此這篇關(guān)于Python Matplotlib初階使用入門的文章就介紹到這了,更多相關(guān)Python Matplotlib使用入門內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
好用的Python編輯器WingIDE的使用經(jīng)驗總結(jié)
WingIDE是個專為python程序語言設(shè)計的集成開發(fā)環(huán)境。從1999年起,Wingware公司便開始專注于python開發(fā),目前WingIDE已經(jīng)是著名的python開發(fā)框架,面向項目風(fēng)格的 IDE 對于大型產(chǎn)品非常有用, 是個很有前途的開發(fā)環(huán)境。2016-08-08
在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
這篇文章主要介紹了在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程,Debian系統(tǒng)和Nginx服務(wù)器皆是高性能的選擇,需要的朋友可以參考下2015-04-04

