11

I have this code:

import matplotlib.pyplot as p1lt

#define data
x = [1, 2, 3, 4, 5, 6]
y = [3, 7, 14, 19, 15, 11]

#create line plot
p1lt.plot(x, y)

#show line plot

p1lt.show()

I get an error that says:

Matplotlib support failed 
Traceback (most recent call last): 
  File "C:\Users\G\AppData\Roaming\JetBrains\IdeaIC2022.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 25, in do_import
    succeeded = activate_func() 
  File "C:\Users\G\AppData\Roaming\JetBrains\IdeaIC2022.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_code_executor.py", line 29, in <lambda>
    "matplotlib": lambda: activate_matplotlib(self.enableGui), 
  File "C:\Users\G\AppData\Roaming\JetBrains\IdeaIC2022.2\plugins\python-ce\helpers\pydev\pydev_ipython\matplotlibtools.py", line 110, in activate_matplotlib
    gui, backend = find_gui_and_backend() 
  File "C:\Users\G\AppData\Roaming\JetBrains\IdeaIC2022.2\plugins\python-ce\helpers\pydev\pydev_ipython\matplotlibtools.py", line 47, in find_gui_and_backend
    backend = matplotlib.rcParams['backend'] 
  File "D:\books\programming\python17\lib\site-packages\matplotlib\__init__.py", line 677, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel) 
  File "D:\books\programming\python17\lib\site-packages\matplotlib\pyplot.py", line 251, in switch_backend
    switch_backend(candidate) 
  File "D:\books\programming\python17\lib\site-packages\matplotlib\pyplot.py", line 266, in switch_backend
    canvas_class = backend_mod.FigureCanvas 
AttributeError: partially initialized module 'matplotlib.backends.backend_macosx' has no attribute 'FigureCanvas' (most likely due to a circular import)
    
Process finished with exit code 0

I used previously Matplotlib on my PC, I removed and reinstalled Python and IntelliJ.

What is wrong? How do I fix it?

paul-g
  • 3,797
  • 2
  • 21
  • 36
Gaff
  • 129
  • 1
  • 1
  • 9
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 16 '22 at 13:09
  • 1
    This kind of error happens when you give your script the same name as a module you're trying to import, or a module used by something you import. – jasonharper Sep 16 '22 at 15:41
  • Is that PC a Mac? Is it ARM-based? Anyway, from the described timeline I'd guess the supposedly reinstalled Python version may have ended up being a different (probably newer) one from the one that worked, and you may have to fiddle around with the versions, via either reinstalling matplotlib too and/or going back to the previous version of Python. It's unlikely that the IDE has anything to do with the issue. – tevemadar Sep 16 '22 at 15:43
  • 3
    @jasonharper the name is not a problem. previosely it was working. now when I turn the console on. it shows these strange error. – Gaff Sep 16 '22 at 16:04
  • 2
    @tevemadar windows 10, python 3.10.7 I reinstalled everything and still have the issue. it only shows the error when the console run is active. – Gaff Sep 16 '22 at 16:07
  • I guess whomever voted to close the question didn't bother to read/understand... if they had, they would have realized the issue is different. I had the same issue on my MacBook Pro M1 with Python 3.10.6. The solution for me was to downgrade from version 3.6 to 3.5: https://stackoverflow.com/a/73755442/596841 – pookie Sep 21 '22 at 20:36
  • 3
    This seems to be a PyCharm bug, see https://intellij-support.jetbrains.com/hc/en-us/community/posts/7670665042450-getting-error-using-matplotlib-while-running-with-console-. As a work-around it proposes "Apart from downgrading matplotlib to 3.5.3, you can disable File | Settings | Tools | Python Scientific | Show plots in tool window." – kadee Oct 10 '22 at 12:39

1 Answers1

17

You can just add those code at the head

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Relax
  • 181
  • 4
  • 7
    can you explain the problem? I down graded the matplotlib, and it resolved the issue. – Gaff Sep 21 '22 at 10:36
  • 1
    I get `ModuleNotFoundError: No module named '_tkinter'` – Boschie Oct 12 '22 at 15:12
  • Got `ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running` – one Oct 21 '22 at 02:26