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

Matplotlib共享X轴的多子图堆叠布局异常问题求助

Matplotlib共享X轴的多子图堆叠布局异常问题求助

我现在在matplotlib的使用上卡壳了😭。我想要做一个三层共享X轴的散点子图,每层对应不同的数据集,用不同颜色的散点展示;但用下面的代码跑出来的结果完全不对——出现了一个空的子图,还有两个子图的内容居然叠在了一起,根本不是我想要的效果。

以下是我用的代码:

f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=False)

ax1 = plt.gca()
ax1.yaxis.set_inverted(True)
ax1.plot(bp_phase, bp_mag, 'o',  markersize=7, markerfacecolor="#BFCFFF",
         markeredgewidth=1, markeredgecolor='black')
ax1.plot(bp_phase_snd_cycle, bp_mag, 'o',  markersize=7, markerfacecolor="#BFCFFF",
         markeredgewidth=1, markeredgecolor='black')
ax2.yaxis.set_inverted(True)             
ax2.plot(g_phase, g_mag,  'o',  markersize=7, markerfacecolor="#99FF99",
         markeredgewidth=1, markeredgecolor='black')
ax2.plot(g_phase_snd_cycle, g_mag, 'o',  markersize=7, markerfacecolor="#99FF99",
         markeredgewidth=1, markeredgecolor='black')
ax3.yaxis.set_inverted(True)             

ax3.plot(rp_phase, rp_mag, 'o',  markersize=7, markerfacecolor="#ffa500",
         markeredgewidth=1, markeredgecolor='black')
ax3.plot(bp_phase_snd_cycle, rp_mag, 'o',  markersize=7, markerfacecolor="#ffa500",
         markeredgewidth=1, markeredgecolor='black')

plt.xticks(fontsize=14, rotation=90)
plt.yticks(fontsize=14)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)

我实在搞不懂为什么会出现空的子图,还把两个绘图内容弄到同一个子图里了。注:我没法提供绘图用的具体数据,或许有没有matplotlib大佬能光看代码就找出我的问题所在呀?

麻烦大家啦,谢谢🙏

备注:内容来源于stack exchange,提问作者user123892

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 17:53:02