0.摘要
本文主要介绍使用matplotlib画图时使用的配置方法,并对配置参数进行解释。
1.pyplot.figure()
用于创建一个新的图。函数参数如下:
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
num:图的标号,不填写则默认自增1.
figsize:图的尺寸。类型为整数构成的元组,顺序为(width,height),单位为英寸,示例figure(figsize=(8,6))
facecolor:背景颜色 。
edgecolor:边框颜色。
dpi:分辨率。类型为整数。
扫描二维码关注公众号,回复:
4547567 查看本文章
2.pyplot.subplot()
在一个 Figure
对象可以包含多个子图(Axes
),调用subplot()即可对一张figure进行划分。下面介绍具体的划分方法:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,101)
y1 = x
y2 = x ** 2
y3 = np.sin(x)
y4 = np.cos(x)
plt.subplot(221)
plt.plot(x,y1)
plt.subplot(222)
plt.plot(x,y2)
plt.subplot(223)
plt.plot(x,y3)
plt.subplot(224)
plt.plot(x,y4)
plt.show()
subplot的行列并非真实的行列,仅是提供给index。不理解也没关系,看下面的例子:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3,101)
y1 = x
y2 = x ** 2
y3 = np.sin(x)
plt.subplot(2,2,1)
plt.plot(x,y1)
plt.subplot(222)
plt.plot(x,y2)
plt.subplot(212)
plt.plot(x,y3)
plt.show()