You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用matplotlib.pyplot将两个绘图设置为一行两列并排布局

实现并排位置图的代码方案

核心逻辑:通过matplotlib创建1行2列的子图实例,分别传入两次vis.plot_positions_dlc调用的ax参数即可实现同画布左右并排展示。

完整实现代码如下:

import matplotlib.pyplot as plt

# 创建1行2列的画布,可调整figsize参数修改画布整体尺寸,单位为英寸
fig, (ax_left, ax_right) = plt.subplots(nrows=1, ncols=2, figsize=(16, 6))

# 左侧绘制普通位置轨迹图
vis.plot_positions_dlc(rois, tracking =tracking_data['ear_left'],
                    roi_colors = ['k','.3','orange','c','c','c','c','r','r','r','r'],
                    xlim=[0,800],ylim=[0,500],linewidth=4, ax=ax_left)
# 可选:给左侧子图加标题
ax_left.set_title("普通位置轨迹图")

# 右侧绘制热力样式位置图
vis.plot_positions_dlc(rois, tracking =tracking_data['ear_left'],
                    roi_colors = ['k','.3','orange','c','c','c','c','r','r','r','r'],
                    xlim=[0,800],ylim=[0,500],linewidth=4, style=heatplot, ax=ax_right)
# 可选:给右侧子图加标题
ax_right.set_title("热力样式位置图")

# 自动调整子图间距,避免标签、标题重叠
plt.tight_layout()
# 展示画布
plt.show()

可选调整说明:

  • 若需要保存生成的图片,可将plt.show()替换为plt.savefig("位置对比图.png", dpi=300),dpi参数越高图片清晰度越高
  • 不需要子图标题的话,直接删掉两行set_title代码即可
  • 可自行调整figsize的两个数值,修改画布的整体宽高比例

内容的提问来源于stack exchange,提问作者Geri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:45:01