3

I try to use matplotlib in my python script but I got this error in the terminal:

Traceback (most recent call last):
  File "graphique.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1048, in <module>
    rcParams = rc_params()
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 897, in rc_params
    fname = matplotlib_fname()
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 748, in matplotlib_fname
    fname = os.path.join(os.getcwd(), 'matplotlibrc')
  File "/home/xavier/anaconda/lib/python2.7/posixpath.py", line 80, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)

Here is my python code, I simply wrote

# -*- coding: utf-8 -*-
import numpy as np
from math import *
import matplotlib.pyplot as plt

What do I have to do?

fonfonx
  • 1,475
  • 21
  • 30
  • If you just start up the interactive interpreter and type `import matplotlib.pyplot`, do you get the same error? – abarnert May 07 '15 at 08:02
  • 1
    Also, what directory are you in when you run this? And what platform are you on? (Looks like linux or *BSD, but I mean specifically what distro and version.) Because it looks like (a) your current working directory has a non-ASCII character in it, and (b) either your locale settings are not UTF-8, or they are but your filesystem isn't. – abarnert May 07 '15 at 08:03
  • @abarnert I am on ubuntu 14.04 and you're right I had an "é" in the name of my directory! I change it and it is working now! Thanks a lot! – fonfonx May 07 '15 at 08:07

1 Answers1

3

The problem is that you have a non-ASCII character in your current working directory.

That actually shouldn't be a problem at all, but it is because of a combination of other things:

  • matplotlib wants to look in your current working directory for a local matplotlibrc file that overrides your default one.
  • Python thinks you're using the C locale instead of the nice UTF-8 locale that Ubuntu 14 should be defaulting to.

So, for a quick workaround, just run your script from a different directory, that has no non-ASCII characters in it.

If you actually want to solve the problem:

  • Make sure you have the latest Ubuntu 14, Anaconda, and matplotlib.
  • From the shell, echo $LANG. The result should be either empty, or something with UTF-8 in it. If not, search AskUbuntu for how to fix that.
  • Make sure your shell and terminal are both set to UTF-8.

From a quick search of the matplotlib issues, this looks like #3516, which looks like it was fixed in #3594, which I think should be in matplotlib 1.4.1+. Also see #3487. So, assuming you $LANG and terminal are correct, and your matplotlib is 1.4.0 or earlier, this is the most likely cause, and updating it (via conda or pip or apt-get or updating Anaconda itself, however you originally installed it) should be the fix.

Or, of course, you can upgrade to Python 3, which will probably either solve the problem, or give you a better error message that tells you exactly what's wrong. (Although, from scanning the issue report, it looks like matplotlib 1.4.0 doesn't have this exact bug in Python 3, only Python 2, as expected… but it may have a related bug…)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • for solving the problem, I just need to do `apt-get update` and `apt-get upgrade`? Both other points are fine (`echo $LANG` returns `fr_FR.UTF-8`, and the terminal is set to UTF-8). – fonfonx May 07 '15 at 08:20
  • @fonfonx: Is Anaconda installed via `apt-get`, and matplotlib either installed via `apt-get` or built-in with Anaconda? – abarnert May 07 '15 at 08:30
  • matplotlib was installed with anaconda, matplotlib version was 1.4.0 so I am updating it – fonfonx May 08 '15 at 05:39