Pandas plot, vars() argument must have __dict__ attribute?

Question:

It was working perfectly earlier but for some reason now I am getting strange errors.

pandas version: 1.2.3

matplotlib version: 3.7.0

sample 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.plot(x='cap', y='Date')
plt.show()
df.dtypes
cap              int64
Date    datetime64[ns]
dtype: object

I get a traceback:

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
Asked By: Sid

||

Answers:

In fact, this problem may cause if you running your code like default script (or in PyCharm interactive console), not in Jupyter.

If it is true, you can fix this error by setting up backend directly in your file with use function:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.use('TkAgg')  # !IMPORTANT

fig, ax = plt.subplots()
res = ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # Plt some data on the axes.o
# plt.show() # optionally show the result.

In some cases TkAgg may not be available. When firstly check, which backend you use. for this, run this simple code:

import matplotlib as mpl
print(mpl.get_backend())

BUT! this must be runned just by your hands in default terminal, outside of PyCharm. (e.g create simple test.py file, paste code, and run python test.py)

Why? Because PyCharm (at least in scientific projects) runs all files with interactive console, where backend module://backend_interagg is used. And this backend causes same error as you have.

So. add mpl.use('TkAgg') in head of your file, or checkout which backend you can use and past those name in this function.

Answered By: NEStenerus nester

The solution by NEStenerus did not work for me, because I don’t have tkinter installed and did not want to change my package configuration.

Alternative Fix

Instead, you can disable the "show plots in tool window" option, by going to
Settings | Tools | Python Scientific | Show plots in tool window and unchecking it.

Answered By: johannesack
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.