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

Pandas绘图报错:vars()参数必须具有__dict__属性

问题:Matplotlib绘图触发TypeError: vars() argument must have dict attribute

环境信息

  • pandas:1.2.3
  • matplotlib:3.7.0
  • Python:3.8

样本数据

DataFrame内容:

df
    cap       Date
0    1     2022-01-04
1    2     2022-01-06
2    3     2022-01-07
3    4     2022-01-08

数据类型:

df.dtypes
cap              int64
Date    datetime64[ns]
dtype: object

执行代码

df.plot(x='cap', y='Date')
plt.show()

报错回溯

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_core.py", line 955, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 61, in plot
    plot_obj.generate()
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 279, in generate
    self._setup_subplots()
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 337, in _setup_subplots
    fig = self.plt.figure(figsize=self.figsize)
  File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 813, in figure
    manager = new_figure_manager(
  File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 382, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 360, in _warn_if_gui_out_of_main_thread
    if _get_required_interactive_framework(_get_backend_mod()):
  File "/Volumes/coding/venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 208, in _get_backend_mod
    switch_backend(rcParams._get("backend"))
  File "/Volumes/coding/venv/lib/python3.8/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

解决方案

1. 调整版本兼容

matplotlib 3.7.0与pandas 1.2.3存在兼容性冲突,可通过以下方式修复:

  • 降级matplotlib到3.6.x版本:
    pip install matplotlib==3.6.3
    
  • 或升级pandas到1.5.x及以上版本:
    pip install pandas==1.5.3
    

2. 指定绘图后端

临时切换到非交互式后端,避免GUI线程问题,修改代码如下:

import matplotlib
# 指定非交互式后端,适合保存图片
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd

# 构建DataFrame
df = pd.DataFrame({
    'cap': [1, 2, 3, 4],
    'Date': pd.to_datetime(['2022-01-04', '2022-01-06', '2022-01-07', '2022-01-08'])
})

df.plot(x='cap', y='Date')
plt.savefig('output_plot.png')  # 保存图片

# 若需交互式显示,可尝试Tk后端(需系统依赖支持)
# matplotlib.use('TkAgg')
# plt.show()

3. 检查线程环境

如果在非主线程中执行绘图代码,需确保绘图操作移至主线程;若在Jupyter环境,添加%matplotlib inline魔法命令可解决部分后端加载问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 19:50:17