Kyle Niemeyer
Kyle Niemeyer

Reputation: 301

Error when trying to set plotting font using Matplotlib

I'm running into a new error with Matplotlib 1.4.2 on Mac OS X (10.10.2) when trying to set the font to Times New Roman in my plot. Here is a MWE that gives an error:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X axis', fontname='Times New Roman')

If I try this, I receive ValueError: failed to convert font family name to ASCII. Adding the label without setting the font works fine.

This issue doesn't seem to be related to the specific font, because trying Helvetica or Arial causes the same issue.

I've tried deleting the font cache in ~/.matplotlib/fontList.cache and then uninstalling and reinstalling Matplotlib, and get the same error. As I expect, when I check the font manager using matplotlib.font_manager.OSXInstalledFonts(), I see my full list of installed fonts.

I'm not sure what change triggered this error (it may be upgrading to 10.10.2), but I was able to set the font this way just fine up until I tried it today—I'm even trying to use previously working code.

Upvotes: 1

Views: 920

Answers (1)

Tom
Tom

Reputation: 2969

I had a similar problem, when I used a fontdict.

I found a solution here: http://matplotlib.1069221.n5.nabble.com/matplotlib-error-ValueError-failed-to-convert-font-family-name-to-ASCII-td44202.html

Find your matplotlibrc file:

import matplotlib
matplotlib.matplotlib_fname()

Open the file and change the graphics back-end, e.g.:
"backend: macosx" -> "backend: TkAgg"

Upvotes: 1

Related Questions