目灵
- 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概述