plt.figure()
创建自定义图像
语法
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
参数
num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸
dpi:指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
实例
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(4,3),facecolor='blue')
plt.show()
plt.subplot()
创建单个子图
语法
subplot(numRows, numCols, plotNum)
参数
numRows:subplot行数
numCols:subplot列数
plotNum:指定创建的Axes对象所在的区域
实例
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 20)
# 绘制图表样式为2X4的图片区域,用坐标表示为(1,1),(1,2),(2,1),(2,2),(3,1),(3,2),(4,1),(4,2),表示的坐标为(1,1),即第一行第一列的子图
plt.subplot(241)
plt.plot(x,x)
plt.show()
output
plt.subplots()
创建多个子图
语法、参数
同plt.subplot()
实例
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10)
#划分子图
fig,axes=plt.subplots(2,1)
ax1=axes[0]
ax2=axes[1]
ax1.plot(x, x)
ax2.plot(x, -x)
plt.show()