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

Pandas箱线图脚本运行正常但调试时抛出TypeError错误

PyCharm调试器运行pandas箱线图脚本报错问题

问题描述

需先创建包含id、nb_inf、Grantham列且数据为整数的.xlsx文件,运行以下最小复现代码:

import matplotlib.pyplot as plt
import pandas as pd

def loader() -> pd.DataFrame:
    df = pd.read_excel("your_file.xlsx", "Feuil1")
    df = df.set_index('id')

    return df

if __name__ == "__main__":
    df: pd.DataFrame = loader()
    for column in df.columns:
        if "Grantham" in column:
            print(column)
            df.boxplot(column=column, by='nb_inf', figsize=(5, 6))
            plt.savefig(f"boxplots/{column}.png")
            plt.close()

使用PyCharm的Run命令运行脚本完全正常,但启动调试器运行时会抛出错误:TypeError: 'NoneType' object is not callable

环境信息:

  • Python 3.10.2
  • PyCharm 2022.1.3(社区版)
  • PyCharm构建详情:
Build #PC-221.5921.27, built on June 21, 2022
Runtime version: 11.0.15+10-b2043.56 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2030M
Cores: 16
Non-Bundled Plugins:
    com.chesterccw.excelreader (2022.1.3)

解决方案

该问题源于旧版本PyCharm调试器与matplotlib/pandas的交互兼容性bug,可尝试以下几种解决方式:

  • 升级PyCharm版本:2022.1.3属于较旧版本,新版本修复了大量调试器与第三方库的兼容性问题,升级到最新社区版即可解决大部分此类冲突。
  • 修改matplotlib后端:在脚本开头添加以下代码,强制使用非交互式后端,避免调试器与matplotlib交互式后端的冲突:
    import matplotlib
    matplotlib.use('Agg')
    
  • 关闭PyCharm调试器的绘图集成:打开PyCharm设置,依次进入Build, Execution, Deployment > Debugger > Data Views > Python,取消勾选Show plots in toolwindow选项,让matplotlib使用独立窗口绘图,避免调试器干扰。
  • 替换箱线图绘制方式:改用seaborn库替代pandas内置的boxplot方法,绕开兼容性问题,示例代码如下:
    import matplotlib.pyplot as plt
    import pandas as pd
    import seaborn as sns
    
    def loader() -> pd.DataFrame:
        df = pd.read_excel("your_file.xlsx", "Feuil1")
        df = df.set_index('id')
        return df
    
    if __name__ == "__main__":
        df: pd.DataFrame = loader()
        for column in df.columns:
            if "Grantham" in column:
                print(column)
                plt.figure(figsize=(5, 6))
                sns.boxplot(x='nb_inf', y=column, data=df.reset_index())
                plt.savefig(f"boxplots/{column}.png")
                plt.close()
    

内容的提问来源于stack exchange,提问作者FluidMechanics Potential Flows

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 01:39:32