多子图:
一、
plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1)
#参数分别为:设定网格(2,2), 当前选中网格(0,1), 跨列和跨行
二、
import matplotlib.gridspec as gridspec
# 指定子图行列数
gs = gridspec(3,3)
ax1 = plt.subplot(gs[0,:]) #第一行跨三列
ax2 = plt.subplot(gs[1, :-1]) # 第二行跨1,2列
ax3 = plt.subplot(gs[1:, -1] # 第三列跨2,3行
三、
fig = plt.figure()
ax1 = fig.add_subplot(331) # 共三行三列第1张图
ax2 = fig.add_subplot(334) # 共3行3列第4张图
四、
plt.gcf() # 获得当前图表的figure对象
plt.gca # 获得子图的Axes对象
plt.subplot(nRows, nCols, plotNum)
# 行数,列数, 子图对象区域
五、
fig, axises = plt.subplots(nrows=1, ncols=1, sharex=False, sharey=False, gridspec_kw=.., figsize=(4,5))
# figsize单位为inch
fig, axises = plt.subplots(2,2)
axises 会得到二行二列二维列表,可以直接引用绘图
axises.flatten() # 踩平列表
plt.subplots_adjust(hspace=0.5, wspace=0.6)
# 调整子图垂直距离和水平距离
其他参考:
# 坐标轴标签
plt.xlabel('variable x')
plt.ylabel('variable y')
# 坐标轴刻度
plt.xlim(-1, 12)
plt.ylim(-1.5, 1.5)
# 坐标轴刻度间隔
from matplotlib.pyplot import MultipleLocator
ax=plt.gca()
ax.yaxis.set_major_locator(MultipleLocator(2.5))
# 字体与字号
matplotlib.rcParams['font.family'] = ['FangSong']
matplotlib.rcParams['font.size'] = 14
plt.rcParams['axes.unicode_minus']=False
# 图表标题
plt.title('三角函数')
# 图例显示
plt.legend(loc='upper right')
# 网络线
plt.grid()
# 注释图表
plt.annotate('sin(x)', (xs[4], ys[4]), (xs[4]+2, ys[4]), arrowprops=dict
(facecolor='red', edgecolor='red', shrink=0.05), color='r')
#柱状图x轴标签
plt.xticks(ticks=xs ,labels=['a','b'], rotation=60, fontsize=12)
# 饼状图
plt.figure(figsize=(16,8))
# 指定脱离中心块
explode = [0.1, 0,0,0,0,0,0,0,0,0,0]
plt.pie(times, explode=explode,labels=mlabels,autopct='%1.1f%
%',shadow=True,startangle=90, radius=1)
plt.legend(loc='upper right')
# 使图例不与图重叠
plt.axis('equal')