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

Matplotlib提示'To output multiple subplots'报错无法绘图怎么解决?

问题原因

报错触发的核心原因是调用pandas的plot.bar绘图方法时,同时指定了ax参数和subplots=True参数,二者逻辑存在冲突:

  • subplots=True的作用是让pandas自动为数据集的每一列创建独立的子图
  • 你已经提前通过plt.subplots手动创建好了两个子图坐标轴,并且通过ax参数将固定坐标轴传递给了plot方法
    两种子图创建逻辑冲突就会触发该报错。

解决方案

直接删除两行plot代码中的subplots=True参数即可,修改后的绘图段代码如下:

fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(12,6))
dfCount.plot.bar(ax=axes[0], xlabel='', legend=None, fontsize=15, rot=0)
dfNPS.plot.bar(ax=axes[1], xlabel='', legend=None, fontsize=15, rot=0)
axes[0].set_title('Quantidade de contratações por plano', fontsize=18)
axes[1].set_title('NPSs de cada Plano', fontsize=18)
plt.show()

如果第一个子图出现过多不符合预期的柱子,可以指定只绘制NPS计数列,调整第一行plot代码为:

dfCount['NPS'].plot.bar(ax=axes[0], xlabel='', legend=None, fontsize=15, rot=0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 14:00:02