目靈
- pyplot API
- 面向對象的API
- figure
- axes
- subplots
import matplotlib.pyplot as plt
plt.close('all')
-
pyplot API
pyplot主要用於交互式繪圖和程序化繪圖生成的簡單案例。每個pyplot函數都會對圖形做一定的修改。
pyplot案例。 -
面向對象的API
matplotlib的核心是面向對象的,建議直接使用對象。
一般創建一個Figure
和一個或多個Axes
。- Figure
注:如果要創建多個圖形,需要顯示調用plt.close()
,使其正確清理內存。
matplotlib.pyplot.figure(num = None,figsize = None,dpi = None,facecolor = None,edgecolor = None,frameon = True,FigureClass = <class’matplotlib.figure.Figure’>,clear = False,** kwargs )
– num:如未提供,創建新的圖形,且圖形編號遞增;如已存在,則激活;
– figsize:(float, float),寬高,默認(6.4, 4.8);
– dpi:圖的分辨率;
– facecolor:背景顏色;
– edgecolor:邊框顏色;
– frameon:默認True,如爲False則禁止繪製圖形框;
– FigureClass:可選自定義實例;
– clear:默認False,如爲True且num數字存在則刪除;
– **kwargs:其它。-
fig.add_axes()
在圖中增加軸。add_axes(rect, projection=None, polar=False, sharex=False, sharey=False, label=None, **kwargs)
– rect: sequence(left, bottom, width, heigh)
– projection: 默認None,{None, ‘ploar’, ‘aitoff’, str, …}
add_axes(axes)x = np.arange(100) y = np.sin(x) fig = plt.figure() ax = fig.add_axes([1, 1, 1, 1]) ax.axes.plot(x, y)
-
fig.add_gridsped()
返回GridSpec父類。指定可以放置子圖的網格的幾何形狀。add_gridspec(nrows, ncols, **kwargs)
x = np.arange(10) y = np.sin(x) fig = plt.figure() gs = fig.add_gridspec(1, 2) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1]) ax1.axes.plot(x) ax2.axes.plot(y) fig.show()
-
fig.add_subplot()
增加一個子圖。add_subplot(self, *args, **kwargs)
add_subplot(nrows, ncols, index, **kwargs)
add_subplot(pos, **kwargs)
add_subplot(ax)
add_subplot()
- Axes
在當前圖上添加軸,並使其成爲當前軸。
不建議:如果圖中已存在一個帶有鍵的軸(args,kwargs),那麼它將簡單地成爲當前軸並返回它。如果要修改軸,必須使用一組唯一的(args, kwargs)。
matplotlib.pyplot.axes(arg = None,polar=None, ** kwargs )
arg: 1)None,添加一個全窗口軸。2)[ left, bottom, width, height ];
polar: True,極座標;
sharex, sharey: 共享x軸、或y軸,該軸具有相同的限制;
axes: Axes,或子類Axes.
**kwargs 提供更多關鍵字參數。# 感受axes的用法 np.random.seed(100) x = np.random.randn(100) y = np.random.randn(100) fig = plt.figure ax1 = plt.axes() ax2 = plt.axes() ax1.plot(x) ax2.plot(y) # 圖1 plt.show() # 圖2 # 修改 ax1 = plt.axes((1, 1, 1, 1)) ax2 = plt.axes((2, 2, 2, 2))
- plt.subplots()參數詳解
創建一個Figure對象和一組子圖。
matplotlib.pyplot.subplots(nrows = 1,ncols = 1,sharex = False,sharey = False,squeeze = True,subplot_kw = None,gridspec_kw = None,** fig_kw )
- Figure
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
Out[7]: Text(0.5, 1.0, 'Simple plot')
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
Out[8]: <matplotlib.collections.PathCollection at 0x7753630>
fig, ax = plt.subplots(2, 2, subplot_kw=dict(polar=True))
ax[0, 0].plot(x, y)
ax[1, 1].scatter(x, y)
Out[9]: <matplotlib.collections.PathCollection at 0xa6cf9b0>

plt.subplots(2, 2, sharex='col')

plt.subplots(2, 2, sharey='row')

plt.subplots(2, 2, sharex='all', sharey='all')

plt.subplots(2, 2, sharex=True, sharey=True)
# No. 10 with a single subplot
# clear=True, if exists delete it.
fig, ax = plt.subplots(num=10, clear=True)
- 附1. matplotlib API概述