基础设置
#准备工作
import matplotlib.pyplot as plt
%matplotlib inline
#让图表直接在jupyter notebook中展示出来
plt.rcParams["font.sans-serif"]='SimHei' # 解决中文乱码问题
plt.rcParams["axes.unicode_minus"]=False #解决负号无法正常显示问题
%config InlineBackend.figure_format='svg'
#设置矢量图格式显示,更加清晰
建立画布与坐标系
法一 add_subplot函数
fig=plt.figure(figsize=(4,3))
ax1=fig.add_subplot(1,1,1)
#在fig2上同时绘制2*2个坐标
fig2=plt.figure()
ax21=fig2.add_subplot(2,2,1)
ax22=fig2.add_subplot(2,2,2)
ax23=fig2.add_subplot(2,2,3)
ax24=fig2.add_subplot(2,2,4)
结果如图:
法二 plt.subplot2grid()函数
不需要手动建立画布 直接调用即可
#基础语句
plt.subplot2grid((2,2),(0,0)) #将整个区域划分为2列2行,并在(0,0)位置作图
#case
import numpy as np
x=np.arange(6)
y=np.arange(6)
plt.subplot2grid((2,2),(0,1))
plt.plot(x,y) #折线图
plt.subplot2grid((2,2),(0,0))
plt.bar(x,y) #折线图
结果如图:
法三plt.subplot函数
plt.subplot(2,2,1)
plt.plot(x,y)
plt.subplot(2,2,4)
plt.bar(x,y)
结果如图:
法四plt.subplots函数
fig,axes=plt.subplots(2,2)
axes[0,0].plot(x,y)
axes[1,0].bar(x,y)
结果如图: