Si vous ne spécifiez pas dans votre script python utilisant matplotlib l'encodage au début du fichier vous pouvez rencontrer l'erreur suivante: SyntaxError: Non-ASCII character '\xc3' surtout dans le cas des accents français. L'exemple suivant:
#!/usr/bin/env pythonimport numpy as npimport matplotlib.pyplot as pltx = np.arange(0,4,0.2)y = np.exp(x)plt.title(u'Un titre très très très très très très très très très très très très long !')plt.grid()plt.plot(x,y)#plt.show()plt.savefig('MatplotlibLongTitle01.png',bbox_inches='tight')
donne l'erreur suivante:
File "TitleMultipleLine.py", line 11SyntaxError: Non-ASCII character '\xc3' in file TitleMultipleLine.py on line 11, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
pour corriger cela il suffit d'ajouter la ligne suivante "# -- coding: utf-8 --" au début du script comme dans cet exemple:
# -*- coding: utf-8 -*-#!/usr/bin/env pythonimport numpy as npimport matplotlib.pyplot as pltx = np.arange(0,4,0.2)y = np.exp(x)plt.title(u'Un titre très très très très très très très très très très très très long !')plt.grid()plt.plot(x,y)#plt.show()plt.savefig('MatplotlibLongTitle01.png',bbox_inches='tight')
Recherches associées
| Liens | Site |
|---|---|
| Python Non-ASCII character '\xc3' | blogspot |
| How to make the python interpreter correctly handle non-ASCII characters in string operations? | stackoverflow |
