subplot2grid()可以通过rowspan和colspan参数让子区跨越固定的网格布局的多个行与列。
subplot2grid()语法格式:
subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)
利用subplot2grid()修改下列代码
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
x = [x for x in range(1, 13)]
y1 = [20, 28, 23, 16, 29, 36, 39, 33, 31, 19, 21, 25]
y2 = [17, 22, 39, 26, 35, 23, 25, 27, 29, 38, 28, 20]
labels = ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7月', '8 月', '9 月', '10 月', '11 月', '12 月']
# 绘制第一个子图
ax1 = plt.subplot2grid((2,4), (0,1), colspan=2)
ax1.plot(x, y1, 'm--o', lw=2, ms=5, label='产品A')
ax1.plot(x, y2, 'g--o', lw=2, ms=5, label='产品B')
ax1.set_title("产品A 与产品B的销售额", fontsize=11)
ax1.set_ylim(10, 45)
ax1.set_ylabel('销售额(亿元)')
ax1.set_xlabel('月

本文介绍了如何在Matplotlib中使用subplot2grid函数创建一个包含两个子图的网格布局,分别展示产品A和产品B的销售额,并展示了饼图表示法。通过colspan和rowspan调整子图跨越网格行和列。
最低0.47元/天 解锁文章
437

被折叠的 条评论
为什么被折叠?



