LateX text rendering is not working with Matplotlib
LateX text rendering is not working with Matplotlib
I am trying this simple code to display a mathematical expression that has variables and their values but I got a big error. it seems that Latex does not work with Matplotlib.
See the picture
The code:
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text', usetex=True)
formula = r'm=fracwg = frac01'.format(100,20)
plt.plot( [0,1,2,3], [0,1,2,3], '.')
plt.text(1,1,r'[]'.format(formula),fontsize=20)
plt.show()
Here is the Error:
"C:Program Files (x86)Python36-32python.exe"
"C:/Users/Techno/PycharmProjects/try/MathExpression .py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Program Files (x86)Python36-32libtkinter__init__.py", line 1702, in __call__
return self.func(*args)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackends_backend_tk.py", line 227, in resize
self.draw()
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibfigure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibimage.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxes_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibimage.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxis.py", line 1192, in draw
renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxis.py", line 1130, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtext.py", line 922, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtext.py", line 309, in _get_layout
ismath=ismath)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_agg.py", line 232, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 365, in make_dvi
texfile], tex)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 335, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:Program Files (x86)Python36-32libsubprocess.py", line 336, in check_output
**kwargs).stdout
File "C:Program Files (x86)Python36-32libsubprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:Program Files (x86)Python36-32libsubprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:Program Files (x86)Python36-32libsubprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Program Files (x86)Python36-32libtkinter__init__.py", line 1702, in __call__
return self.func(*args)
File "C:Program Files (x86)Python36-32libtkinter__init__.py", line 746, in callit
func(*args)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackends_backend_tk.py", line 310, in idle_draw
self.draw()
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibfigure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibimage.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxes_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibimage.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibartist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxis.py", line 1192, in draw
renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibaxis.py", line 1130, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtext.py", line 922, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtext.py", line 309, in _get_layout
ismath=ismath)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibbackendsbackend_agg.py", line 232, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 365, in make_dvi
texfile], tex)
File "C:Program Files (x86)Python36-32libsite-packagesmatplotlibtexmanager.py", line 335, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:Program Files (x86)Python36-32libsubprocess.py", line 336, in check_output
**kwargs).stdout
File "C:Program Files (x86)Python36-32libsubprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:Program Files (x86)Python36-32libsubprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:Program Files (x86)Python36-32libsubprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 0
1 Answer
1
I just tried running your code, and got this as a result:
Assuming this is the output you wanted, this means your code is written correctly, and your problem is likely with how Python is installed or configured in your system.
As your error message points out to something going wrong with Tkinter, there is a chance your graphics backend might the culprit, so that might be a good place to start investigating.
Over here, I run Python 3.6.5 64-bit from the Anaconda Distribution installation, and use Spyder as my IDE. In Spyder, I can change my graphics backend between Tkinter (amongst others) by going to Tools
- Preferences
- IPython Console
- Graphics
- Graphics backend
. I tried running your code with my backend set to Tkinter
and automatic
, and it worked fine in both cases. I can see from your screenshot you're not using Spyder, but I hope this might help you find your own way through your system.
Tools
Preferences
IPython Console
Graphics
Graphics backend
Tkinter
automatic
I ran the code using Python IDL and I got the same error
– abduallah adi
Aug 22 at 2:06
If I change rc('text', usetex=True) to "False" it will work but the output will be m=fracwg = frac01
– abduallah adi
Aug 22 at 2:12
I'm afraid I am not familiar with PyCharm nor Python IDL, so I can't help you much there. I would suggest trying to google about how Tkinter works with PyCharm; maybe it needs some setup instructions before it starts working? Or try finding out how to change your graphics backend in PyCharm to something else, like Qt5.
– fabio_hirono
Aug 22 at 2:32
Setting
usetex=False
will disable the Latex interpreter in Matplotlib, so your string will be parsed literally instead of interpreted as Latex commands - so yeah, in that case your should see exactly what you type popping up in your figure.– fabio_hirono
Aug 22 at 2:32
usetex=False
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you for your respond. I am using PyCharm as IDE and I have PyQt5 included.I cannot find such option in pycharm
– abduallah adi
Aug 22 at 1:39