多子图与复合布局

学习 subplot、GridSpec 与双坐标轴等布局技巧,在一张画布上讲述多个数据故事。

subplotGridSpec双坐标轴组合图
季度销量双子图

图表作品

共 5 张图表 · 点击图片可放大,展开可查看完整代码

01

季度销量双子图

用 subplot 上下排列两组数据,分别展示柱状图与折线图。

查看 Python 代码 +
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
x_quarter = ['第一季度','第二季度','第三季度','第四季度'] 
y_watch = np.array([120, 150, 130, 180]) 
y_headset = np.array([200, 180, 220, 250])
ax1 = plt.subplot(211)
bar_width = 0.3
ax1.bar(x_quarter,y_watch,width = bar_width)
ax1.set_title("智能手表季度销量", fontsize=11)
ax1.set_ylabel('销量(万台)')
ax2 = plt.subplot(212)
ax2.plot(x_quarter,y_headset)
ax2.set_title("蓝牙耳机季度销量", fontsize=11)
ax2.set_xlabel('季度')
ax2.set_ylabel('销量(万台)')
plt.tight_layout()
plt.show()
02

各平台用户性别对比

用两个共享 y 轴的水平条形图,对比不同平台的用户结构。

查看 Python 代码 +
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
platforms = ['微博', 'B站', '抖音', '小红书', '知乎'] 
y_platforms = np.arange(len(platforms)) 
male_users = np.array([55, 45, 50, 30, 60]) 
female_users = np.array([45, 55, 50, 70, 40])
fig, (ax1, ax2) = plt.subplots(1,2,sharey = True)
bar_height = 0.3
ax1.barh(y_platforms,width = male_users,tick_label = platforms,height = bar_height)
ax1.set_title("各平台男性用户(百万)", fontsize=11)
ax1.set_xlabel("用户数 (百万)",fontsize=11)
ax1.set_xlim(0, 80)
ax2.barh(y_platforms,width = female_users,tick_label = platforms,height = bar_height)
ax2.set_title("各平台女性用户(百万)", fontsize=11)
ax2.set_xlabel("用户数 (百万)",fontsize=11)
ax2.set_xlim(0, 80)
plt.tight_layout()
plt.show()
03

流量来源与访问量组合图

借助 subplot2grid 将两张饼图与一张柱状图组合在一张画布中。

查看 Python 代码 +
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
months_growth = ['一月', '二月', '三月', '四月', '五月', '六月'] 
x_months_growth = np.arange(len(months_growth)) 
visits = np.array([1.5, 1.8, 2.2, 2.0, 2.5, 2.8]) 
source_labels = ['直接访问', '搜索引擎', '社交媒体', '推荐链接'] 
source_2023 = np.array([20, 40, 30, 10]) 
source_2024 = np.array([15, 35, 40, 10])
ax1 = plt.subplot2grid((2,2),(0,0))
ax1.pie(source_2023,labels = source_labels)
ax1.set_title("2023年网站流量来源分布",fontsize=11)
ax2 = plt.subplot2grid((2,2),(1,0))
ax2.pie(source_2024,labels = source_labels)
ax2.set_title("2024年网站流量来源分布",fontsize=11)
ax3 = plt.subplot2grid((2,2),(0,1),rowspan = 2)
bar_width = 0.3
ax3.bar(x_months_growth,visits,width = bar_width,tick_label = months_growth)
ax3.set_title("2024年上半年月度网站访问量",fontsize=11)
plt.tight_layout()
plt.show()
04

股价与成交量双轴图

用 twinx 实现柱线组合,在同一张图上用双坐标轴分别展示成交量与股价。

查看 Python 代码 +
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
month_names = ['一月', '二月', '三月', '四月', '五月', '六月']
x_axis_months = np.arange(len(month_names)) 
stock_price = np.array([100, 105, 115, 110, 120, 135]) 
trade_volume = np.array([1.2, 1.5, 2.0, 1.8, 2.5, 3.0])
fig, ax1 = plt.subplots()
ax1.bar(x_axis_months,trade_volume,tick_label = month_names,color = 'tab:blue')
ax1.set_ylabel('成交量(百万股)')
ax2 = ax1.twinx()
ax2.plot(x_axis_months,stock_price,'r-o')
ax1.set_ylabel('股价(元)')
plt.tight_layout()
plt.show()
05

招生数据复合布局

使用 GridSpec 将趋势线、饼图与柱状图组织成层次清晰的版面。

查看 Python 代码 +
%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ["SimHei"]
years = ['2021年', '2022年', '2023年', '2024年'] 
total_enrollment = np.array([20000, 21500, 23000, 24000]) 
colleges = ['工学院', '文学院', '商学院', '理学院'] 
enrollment_by_college = np.array([8000, 5000, 6000, 4000])
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(2,2)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(years,total_enrollment)
ax1.set_title('2021—2024年大学年度招生总人数变化趋势',fontsize=11)
ax2 = fig.add_subplot(gs[1, 0])
ax2.pie(enrollment_by_college,labels = colleges)
ax2.set_title('各学院招生人数占比分布',fontsize=11)
ax3 = fig.add_subplot(gs[1, 1])
ax3.bar(colleges,enrollment_by_college)
ax3.set_title('各学院招生人数对比',fontsize=11)
plt.show()