概覽
-
The pyplot API
matplotlib.pyplot 是一個像Matlab一樣工作的命令集合,每一個pyplot函數(shù)都對圖標(biāo)做出一些變更: 創(chuàng)建圖表/創(chuàng)建圖表繪制區(qū)域/繪制線條/繪制標(biāo)簽,`pyplot 的目的是交互式繪制和簡單的自動化繪制。
-
The object-oriented API
Matplotlib的核心是面向?qū)ο蟮?,如果需要更多的控制和自定義繪制,我們建議直接使用對象工作。
很多情況下,你可以使用pyplot.subplots創(chuàng)建一個Figure和很多的Axes, 然后使用這些對象工作,同樣也可以顯式的創(chuàng)建Figure(GUI應(yīng)用中)。
-
The pylab API(不贊成)
matplotlib.pylab模塊在一個單獨的命名空間包含 matplotlib.pyplot, numpy 和 其他的函數(shù),初始目的是 通過導(dǎo)入所有的函數(shù)到全局命名空間來模仿一個類Matlab的工作方式。
API
-
圖表 matplot.figure.Figure
所有繪圖元素最頂層的容器
圖表實例支持回調(diào),通過callbacks(CallbackRegistry實例) 屬性
import matplotlib.pyplot as plt
# 創(chuàng)建一個Figure
fig = plt.figure()
plt.plot()
plt.show()
-
plot
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
繪制線和標(biāo)記
調(diào)用方法
plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
點或線的坐標(biāo)通過x/y給出,
可選參數(shù)fmt是一個字符串標(biāo)識,可以方便的定義顏色、標(biāo)記、線型
plot(x, y) # plot x and y using default line style and color
plot(x, y, 'bo') # plot x and y using blue circle markers
plot(y) # plot y using x as index array 0..N-1
plot(y, 'r+') # ditto, but with red plusses
可以使用Line2D屬性作為關(guān)鍵字在外形上控制更多,線屬性和fmt可以混合使用
plot(x, y, 'go--', linewidth=2, markersize=12)`
plot(x, y, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12)
使用fmt時,關(guān)鍵字參數(shù)優(yōu)先生效
參數(shù)
x y
可以是數(shù)組或整數(shù),x是可選的,若無值則默認[0 ,..., N-1]
fmt
可選的字符串,是快速設(shè)置線的屬性的一個縮寫,所有屬性均可以由關(guān)鍵字參數(shù)控制
data
可索引對象,可選,標(biāo)簽數(shù)據(jù)對象,提供標(biāo)簽名稱以繪制x y 坐標(biāo)軸
返回值
lines
代表繪制數(shù)據(jù)的Line2D對象列表
fmt
包含顏色、標(biāo)記、線的格式化字符串,每一項都是可選的,如果沒有提供某項則使用周期循環(huán)中的值
fmt = '[color][marker][line]'
color 支持的顏色
| character | color |
|---|---|
| 'b' | blue |
| 'g' | green |
| 'r' | red |
| 'c' | cyan |
| 'm' | magenta |
| 'y' | yellow |
| 'k' | black |
| 'w' | white |
Markers
| character | description |
|---|---|
| '.' | point marker |
| ',' | pixel marker |
| 'o' | circle marker |
| 'v' | triangle_down marker |
| '^' | triangle_up marker |
| '<' | triangle_left marker |
| '>' | triangle_right marker |
| '1' | tri_down marker |
| '2' | tri_up marker |
| '3' | tri_left marker |
| '4' | tri_right marker |
| 's' | square marker |
| 'p' | pentagon marker |
| '*' | star marker |
| 'h' | hexagon1 marker |
| 'H' | hexagon2 marker |
| '+' | plus marker |
| 'x' | x marker |
| 'D' | diamond marker |
| 'd' | thin_diamond marker |
| '|' | vline marker |
| '_' | hline marker |
Line Style
| character | description |
|---|---|
| '-' | solid line style |
| '--' | dashed line style |
| '-.' | dash-dot line style |
| ':' | dotted line style |
-
subplots
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False,squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
創(chuàng)建一個圖表和一組繪圖區(qū)域
nrows, ncols 繪圖區(qū)域網(wǎng)格的行列,默認1行1列
sharex, sharey: bool or {'none', 'all', 'row', 'col'}, default: False 控制多個繪圖區(qū)域是否共用x、y坐標(biāo)軸屬性
-
Axes
class matplotlib.axes.Axes(fig, rect, facecolor=None, frameon=True, sharex=None, sharey=None, label='', xscale=None, yscale=None, **kwargs)
Axes 包含很多元素:坐標(biāo)軸、刻度、2D線、文本、多邊形