Issue
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
Solution
In fact, this problem may be caused by 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 run 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 the interactive console, where backend module://backend_interagg
is used. And this backend causes same error as you have.
So. add mpl.use('TkAgg')
in the head of your file, or checkout which backend you can use and paste those names in this function.
Answered By - NEStenerus nester
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.