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

Pandas绘制alpha与fluxes时遇TypeError: vars()参数需含__dict__属性

问题描述

尝试在Pandas DataFrame all_fluxes中绘制alpha与fluxes的关系,DataFrame示例如下:

all_fluxes
Out[47]: 
      reaction    fluxes  alpha         Gene
0       CYTDK2  0.000000    0.0          MDH
1         XPPT  0.000000    0.0          MDH
2        HXPRT  0.000000    0.0          MDH
3        NDPK5  0.000000    0.0          MDH
4       SHK3Dr  0.312780    0.0          MDH
        ...       ...    ...          ...
2711    OCTNLL  0.000000    1.0          PHI
2712  EX_thf_c -0.000503    1.0          PHI
2713  EX_nad_c -0.001712    1.0          PHI
2714       HPS  0.000000    1.0          PHI
2715       PHI  0.000000    1.0          PHI

执行绘图代码:

all_fluxes.plot(x='alpha',y='fluxes')

出现以下错误:

Traceback (most recent call last):
  File "C:\Users\MyName\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3460, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-46-cac32768086e>", line 1, in <module>
    all_fluxes.plot(x='alpha',y='fluxes')
  File "C:\Users\MyName\anaconda3\lib\site-packages\pandas\plotting\_core.py", line 1000, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File "C:\Users\MyName\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\__init__.py", line 71, in plot
    plot_obj.generate()
  File "C:\Users\MyName\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 451, in generate
    self._setup_subplots()
  File "C:\Users\MyName\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 512, in _setup_subplots
    fig = self.plt.figure(figsize=self.figsize)
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\_api\deprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 813, in figure
    manager = new_figure_manager(
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 382, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 360, in _warn_if_gui_out_of_main_thread
    if _get_required_interactive_framework(_get_backend_mod()):
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 208, in _get_backend_mod
    switch_backend(rcParams._get("backend"))
  File "C:\Users\MyName\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 331, in switch_backend
    manager_pyplot_show = vars(manager_class).get("pyplot_show")
TypeError: vars() argument must have __dict__ attribute
C:\Program Files\JetBrains\PyCharm 2022.1.2\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_utils.py:606: FutureWarning: iteritems is deprecated and will be removed in a future version. Use .items instead.
  for item in s.iteritems():
解决方案
  • 切换Matplotlib后端:在代码开头指定后端,避开GUI线程冲突问题
import matplotlib
matplotlib.use('TkAgg') # 或根据环境选择'Agg'、'Qt5Agg'等
import matplotlib.pyplot as plt
import pandas as pd

# 后续执行绘图代码
all_fluxes.plot(x='alpha', y='fluxes')
plt.show()
  • 改用Matplotlib原生绘图:绕过Pandas封装的plot方法,直接调用Matplotlib接口
import matplotlib.pyplot as plt

plt.scatter(all_fluxes['alpha'], all_fluxes['fluxes'])
plt.xlabel('alpha')
plt.ylabel('fluxes')
plt.show()
  • 调整PyCharm设置:关闭内置绘图工具窗口,避免后端冲突

    1. 打开File > Settings > Tools > Python Scientific
    2. 取消勾选"Show plots in toolwindow"
    3. 重启PyCharm后重新运行代码
  • 升级依赖库:该错误可能源于版本兼容问题,执行命令更新相关包

pip install --upgrade pandas matplotlib

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 16:37:31