Unicode troubleshooting when trying to save a picture with latex text in Matplotlib

Asked

Viewed 91 times

0

Talk personal. I have a problem using the Matplotlib library. Before the last Windows update I used the library without any problem. I basically use it to create function charts and save them on. pdf using Latex formatting for the text, below follows a code as an example:

import numpy as np
from matplotlib import pyplot as plt

# Data for plot
step = 0.001
x = np.arange(0, 2 + step, step)
y = x*x*np.cos(10*x)

plt.rc('text', usetex=True)
 plt.rc('font', family='serif')
plt.figure(figsize=(19.2, 10.8))

plt.plot(x, y, lw=2)
plt.title(r'Function $f(x) = x^2 \cos(10x)$')
plt.savefig('plot.pdf', bbox_inches='tight')
plt.show()
plt.close()

However after the update my old windows bugged and I was forced to format the pc, and now I can’t use the same code above and save the figures in . pdf, the following error happens:

File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py", line 1057, in find_tex_file
 return result.decode('ascii')

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)

Using the same code I am able to save in other formats, for example . png and .eps. After formatting I basically installed the same software I used before.

Ps. The above code has been tested on Spyder 3.2.6 and Pycharm 2018.1.3 with Python version 3.6.4. Latex is working perfectly. I use Miktex for Latex base and Texstudio editor.

  • Unfortunately an extra space came out in the eighth line of the code. Disregard.

2 answers

1

Probably the plot.pdf contains accents, maybe setting yourself at the top of the document:

# -*- coding: utf-8 -*-

import numpy as np
from matplotlib import pyplot as plt
...

If you are running via cmd run before running your script:

chcp 65001

As the example in https://matplotlib.org/examples/text_labels_and_annotations/unicode_demo.html

For utf-8 you may have to add as well:

from __future__ import unicode_literals

Should stay like this:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import numpy as np

from matplotlib import pyplot as plt
...
  • William thank you very much for the answer. Unfortunately the addition of the indicated configuration did not take effect.

  • @Fábionascentes tries it at the beginning of the script: from __future__ import unicode_literals, edited the answer with an example

  • William tested here and unfortunately did not compile.

  • @Fábionascentes same error codec can't decode byte?

  • That’s right, sorry I forgot to quote. Same mistake.

  • @Fábionascentes tries so, change the plt.rc('font', family='serif') for plt.rc('font', **{'sans-serif' : 'Arial', 'family' : 'sans-serif'}) and see if it has an effect

  • William not yet. Same mistake.

Show 2 more comments

0


So after a lot of suffering I managed to solve the problem.

To use latex, the matplotlib library uses the log path to access the Miktex workplace. It turns out that the username I was using was Fabio, with the accent. Then when the library tried to access the path it found a path, which when used to export the figure, resulted in the Unicodedecorror.

So I created another Fabio user, deleted the old one and reinstalled Miktex. After that the error ceased.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.