matplotlib使用教程(二):Axes和subplot

這一系列文章原載於公眾號

工程師milte

r,如果文章對大家有幫助,懇請大家動手關注下哈~

今天繼續分析matplotlib中的基本概念。

matplotlib使用教程(二):Axes和subplot

在上一篇文章中,我們獲得了一個figure,並且也大致瞭解了一下figure的相關設定項。

現在,我們要在figure上畫圖了。你本來以為直接在figure上畫就行了,但事實沒這麼簡單。matplotlib允許我們將一個figure透過柵格系統劃分成不同的格子,然後在格子中畫圖,這樣就可以在一個figure中畫多個圖了。這裡的每個格子有兩個名稱:Axes和subplot。subplot是從figure所有的格子來看的。因為figure要統一管理協調這些格子的位置、間隔等屬性,管理協調的方法和屬性設定就在subplots的層面進行。

Axes是從作為畫圖者的我們的角度來定義的,我們要畫的點、線等都在Axes這個層面來進行。畫圖用的座標系統自然也是在Axes中來設定的。

搞清楚這兩個概念後,我們就來看看如何將figure劃分格子,並獲得我們畫圖使用的Axes。

透過下邊的程式碼,我們將整個fig劃分成了2x2 4個subplots。返回給我們的自然是四個axes,可以透過檢視axes證實:

axes = fig。subplots(2,2)

matplotlib使用教程(二):Axes和subplot

在上圖裡,我們看到返回的物件是AxesSubplot,它實質上是包含了Axes的Subplot。在使用上,我們完全可以把它當做Axes使用。

如果我們只想在figure上畫一幅圖,就有兩種方法:

axes = fig。subplots(1,1) or axes = fig。subplots()

此時得到的axes是就是一個AxesSubplot物件。

matplotlib使用教程(二):Axes和subplot

如果大家觀察仔細,會看到裡面有3個值,它們確定了subplot在figure中的位置。可以透過下圖感受到:

fig = plt。figure()fig。set_facecolor(“green”)axis = fig。subplots()plt。show()

matplotlib使用教程(二):Axes和subplot

前兩個值實際上是座標原點相對於figure左下角的位置。第三個值是subplot的寬和高。

figure中還有一個方法:add_subplot。其目的也是將figure劃分成柵格,並獲取其中某一個。使用方法如下所示:

fig = plt。figure()ax1 = fig。add_subplot(2, 3, 1) fig。add_subplot(232, facecolor=“blue”) fig。add_subplot(233, facecolor=“yellow”) fig。add_subplot(234, sharex=ax1) fig。add_subplot(235, facecolor=“red”) fig。add_subplot(236, facecolor=“green”) plt。show()

matplotlib使用教程(二):Axes和subplot

這裡有兩個地方需要注意一下。add_subplot(232)和add_subplot(2,3,2)等價的。

另外,如果將最後一個236改成436,你猜會發生什麼呢?

答案是如下所示:

matplotlib使用教程(二):Axes和subplot

可以看到436 相當於將figure重新劃分了網格,並將第6個網格設定成綠色。

兩種不同的網格劃分產生了重疊。這再次體現了matplotlib的靈活性。

最佳的實踐是:在開始時就將figure的網格劃分好,並不再改變。

Axes 概覽

最後,我們還是通覽一下Axes的屬性:

{‘figure’:

, ‘_subplotspec’: , ‘figbox’: Bbox([[0。125, 0。125], [0。9, 0。88]]), ‘rowNum’: 0, ‘colNum’: 0, ‘numRows’: 1, ‘numCols’: 1, ‘_stale’: True, ‘stale_callback’: , ‘_axes’: , ‘_transform’: None, ‘_transformSet’: False, ‘_visible’: True, ‘_animated’: False, ‘_alpha’: None, ‘clipbox’: None, ‘_clippath’: None, ‘_clipon’: True, ‘_label’: ‘’, ‘_picker’: None, ‘_contains’: None, ‘_rasterized’: None, ‘_agg_filter’: None, ‘_mouseover’: False, ‘eventson’: False, ‘_oid’: 0, ‘_propobservers’: {}, ‘_remove_method’: >, ‘_url’: None, ‘_gid’: None, ‘_snap’: None, ‘_sketch’: None, ‘_path_effects’: [], ‘_sticky_edges’: _XYPair(x=[], y=[]), ‘_in_layout’: True, ‘_position’: Bbox([[0。125, 0。125], [0。9, 0。88]]), ‘_originalPosition’: Bbox([[0。125, 0。125], [0。9, 0。88]]), ‘_aspect’: ‘auto’, ‘_adjustable’: ‘box’, ‘_anchor’: ‘C’, ‘_sharex’: None, ‘_sharey’: None, ‘bbox’: , ‘dataLim’: Bbox([[inf, inf], [-inf, -inf]]), ‘viewLim’: Bbox([[0。0, 0。0], [1。0, 1。0]]), ‘transScale’: , ‘transAxes’: , ‘transLimits’: , ‘transData’: , ‘_xaxis_transform’: , ‘_yaxis_transform’: , ‘_axes_locator’: None, ‘spines’: OrderedDict([(‘left’, ), (‘right’, ), (‘bottom’, ), (‘top’, )]), ‘xaxis’: , ‘yaxis’: , ‘_facecolor’: ‘white’, ‘_frameon’: True, ‘_axisbelow’: ‘line’, ‘_rasterization_zorder’: None, ‘_connected’: {}, ‘ignore_existing_data_limits’: True, ‘callbacks’: , ‘_autoscaleXon’: True, ‘_autoscaleYon’: True, ‘_xmargin’: 0。05, ‘_ymargin’: 0。05, ‘_tight’: None, ‘_use_sticky_edges’: True, ‘_get_lines’: , ‘_get_patches_for_fill’: , ‘_gridOn’: False, ‘lines’: [], ‘patches’: [], ‘texts’: [], ‘tables’: [], ‘artists’: [], ‘images’: [], ‘_mouseover_set’: , ‘child_axes’: [], ‘_current_image’: None, ‘legend_’: None, ‘collections’: [], ‘containers’: [], ‘title’: Text(0。5, 1, ‘’), ‘_left_title’: Text(0。0, 1, ‘’), ‘_right_title’: Text(1。0, 1, ‘’), ‘titleOffsetTrans’: , ‘_autotitlepos’: True, ‘patch’: , ‘axison’: True, ‘fmt_xdata’: None, ‘fmt_ydata’: None, ‘_navigate’: True, ‘_navigate_mode’: None, ‘_xcid’: 0, ‘_ycid’: 0, ‘_layoutbox’: None, ‘_poslayoutbox’: None}

可以看到,裡面有Axes在網格系統中的座標,所屬的figure,還有title,_facecolor等屬性。yaxis、xaxis代表座標軸。

此時,建議大家對比一下Axes和上一講中的Figure的屬性。就可以大致有個感覺,什麼樣的設定要在哪裡去設。