Ravexina
Ravexina

Reputation: 2848

Can't use pyplot due to _tkinter.TclError: unknown color name

I've got a simple code:

from matplotlib import pyplot as plt
plt.plot([1,2,5])
plt.show()

It works fine in jupyter notebook, however when I try to run it using command line:

$ python3 main.py

It throws an error:

_tkinter.TclError: unknown color name "[97]#282a36"

The whole trackeback:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    plt.plot([1,2,5])
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2811, in plot
    return gca().plot(
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 935, in gca
    return gcf().gca(**kwargs)
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 578, in gcf
    return figure()
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 525, in figure
    **kwargs)
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 3218, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py", line 1008, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: unknown color name "[97]#282a36"

I already tried changing matplotlib's backend:

import matplotlib
matplotlib.use('pdf') # Or using other arguments matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1,2,5])
plt.show()

It gives me the same error message.

I also tried installing matplotlib using pip and my distribution package manager, both giving me the same error.

tkinter has been installed from my distribution repositories.

Any suggest would be helpful, I couldn't find any solution on similar questions.

Upvotes: 0

Views: 5135

Answers (1)

Ravexina
Ravexina

Reputation: 2848

After reading this issue on matplotlib's Github page, I emptied out my .Xresources and it fixed the issue.

So I looked a little bit more into the .Xresources file and I found out a line:

*background:   [97]#282a36

Which was the cause of matplotlib complaining about a color nameed: [97]#282a36:

_tkinter.TclError: unknown color name "[97]#282a36"

Removing [97] from the line fixed the issue. remember that you have to run:

xrdb -merge .Xresources

To make the changes take place.

Upvotes: 2

Related Questions