當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python matplotlib subplot用法及代碼示例

本文簡要介紹 python 語言中 matplotlib.pyplot.subplot 的用法。

用法

matplotlib.pyplot.subplot(*args, **kwargs)

將軸添加到當前圖窗或檢索現有軸。

這是 Figure.add_subplot 的包裝器,它在使用隱式 API 時提供額外的行為(請參閱注釋部分)。

調用簽名:

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)
參數
*args int, (int, int, index ), 或 SubplotSpec , 默認: (1, 1, 1)

由其中之一說明的子圖的位置

  • 三個整數(nrowsncolsindex)。子圖將在具有nrows 行和ncols 列的網格上占據index 位置。 index 從左上角的 1 開始,向右遞增。 index 也可以是一個二元組,指定子圖的(firstlast)索引(從 1 開始,包括 last),例如,fig.add_subplot(3, 1, (1, 2)) 生成一個跨越上 2 的子圖/3 圖。

  • 一個 3 位整數。這些數字被解釋為好像分別作為三個 single-digit 整數給出,即 fig.add_subplot(235)fig.add_subplot(2, 3, 5) 相同。請注意,這隻能在不超過 9 個子圖的情況下使用。

  • 一個 SubplotSpec

projection {無,'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', 'rectilinear',str},可選

子圖的投影類型 ( Axes )。 str 是自定義投影的名稱,請參閱 projections 。默認無導致 'rectilinear' 投影。

polar 布爾值,默認值:假

如果為真,則相當於投影='polar'。

sharex, sharey Axes ,可選

與 sharex 和/或 sharey 共享 x 或 y axis 。該軸將具有與共享軸的軸相同的限製、刻度和比例。

label str

返回軸的標簽。

返回
Axes

子圖的軸。返回的 Axes 實際上可以是子類的實例,例如極坐標投影的 projections.polar.PolarAxes

其他參數
**kwargs

此方法還采用返回軸基類的關鍵字參數;除了 figure 參數。直線基類 Axes 的關鍵字參數可以在下表中找到,但如果使用另一個投影,也可能存在其他關鍵字參數。

屬性

說明

adjustable

{'box', 'datalim'}

agg_filter

一個過濾器函數,它接受一個 (m, n, 3) 浮點數組和一個 dpi 值,並返回一個 (m, n, 3) 數組和距圖像左下角的兩個偏移量

alpha

標量或無

anchor

(浮點數、浮點數)或 {'C'、'SW'、'S'、'SE'、'E'、'NE'、...}

animated

bool

aspect

{'auto', 'equal'} 或浮點數

autoscale_on

bool

autoscalex_on

unknown

autoscaley_on

unknown

axes_locator

可調用[[軸,渲染器],Bbox]

axisbelow

布爾或'line'

box_aspect

浮點數或無

clip_box

BboxBase 或無

clip_on

bool

clip_path

補丁或(路徑,變換)或無

facecolor 或 fc

color

figure

Figure

frame_on

bool

gid

str

in_layout

bool

label

object

mouseover

bool

navigate

bool

navigate_mode

unknown

path_effects

AbstractPathEffect 列表

picker

None 或 bool 或 float 或可調用

position

[左、下、寬、高]或 Bbox

matplotlib.axes.Axes.set_prop_cycle

Cycler

rasterization_zorder

浮點數或無

rasterized

bool

sketch_params

(比例:浮點數,長度:浮點數,隨機性:浮點數)

snap

布爾或無

subplotspec

unknown

title

str

transform

Transform

url

str

visible

bool

xbound

(下:浮點數,上:浮點數)

xlabel

str

matplotlib.axes.Axes.set_xlim

(左:浮點數,右:浮點數)

xmargin

浮點數大於 -0.5

xscale

unknown

xticklabels

unknown

xticks

unknown

ybound

(下:浮點數,上:浮點數)

ylabel

str

matplotlib.axes.Axes.set_ylim

(底部:浮點數,頂部:浮點數)

ymargin

浮點數大於 -0.5

yscale

unknown

yticklabels

unknown

yticks

unknown

zorder

float

注意

創建新的軸將刪除與其共享邊界之外的任何先前存在的軸:

import matplotlib.pyplot as plt
# plot a line, implicitly creating a subplot(111)
plt.plot([1, 2, 3])
# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its axes) previously created, will be removed
plt.subplot(211)

如果您不希望出現這種情況,請改用 Figure.add_subplot 方法或 pyplot.axes 函數。

如果沒有傳遞 kwargs 並且在 args 指定的位置存在軸,則將返回該軸,而不是創建新的軸。

如果通過kwargs並且在args指定的位置存在Axes,則投影類型相同,且kwargs與現有Axes匹配,則返回現有Axes。否則,將使用指定的參數創建一個新軸。我們保存了對 kwargs 的引用,用於此比較。如果 kwargs 中的任何值是可變的,我們將不會檢測到它們發生突變的情況。在這些情況下,我們建議使用 Figure.add_subplot 和顯式 Axes API 而不是隱式 pyplot API。

例子

plt.subplot(221)

# equivalent but more general
ax1 = plt.subplot(2, 2, 1)

# add a subplot with no frame
ax2 = plt.subplot(222, frameon=False)

# add a polar subplot
plt.subplot(223, projection='polar')

# add a red subplot that shares the x-axis with ax1
plt.subplot(224, sharex=ax1, facecolor='red')

# delete ax2 from the figure
plt.delaxes(ax2)

# add ax2 to the figure again
plt.subplot(ax2)

# make the first axes "current" again
plt.subplot(221)

相關用法


注:本文由純淨天空篩選整理自skytowner.com大神的英文原創作品 matplotlib.pyplot.subplot。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。