简体   繁体   中英

Matplotlib and non-ascii characters

Never ending story about Matplotlib and non-ascii characters continues, indeed:

1) Localhost: Mac OS X: By default, some characters are missing (replaced with []). OK, after adding matplotlib.rc('font', family='Verdana') characters are displayed fine. However, when deploying to a production server things get broken once again.

2) Server: Linux (openSUSE, using: matplotlib.use('Agg') , serving png's and pdf's, Django app): Calling matplotlib.rc('font', family='Verdana') does not help now (WHY?). I even copied some core fonts from Mac to openSUSE to make sure they are the same.

What does help a bit is this:

import matplotlib.font_manager as fm
fp = fm.FontProperties(fname="/usr/share/fonts/truetype/Verdana.ttf") 
# matplotlib.rcParams['font.family'] = fp.get_name()
matplotlib.rc('font', family=fp.get_name())

and then using per partes plt.title(_title, fontproperties=fp) for each plot/block. This is very tiring… And what is worse, it is not usable when working with legends. For example there is no way (is there?) to use it like that:

ax.legend([charts], [_label], fontproperties=fp) 

and

ax.legend([charts], [_label], prop={'family': 'Verdana'}

does not have any effect (it works on Mac OS X, though).

Funny thing is that most of non-ascii characters are displayed fine (ščřž…), problems occur only with ě, ť, ď… Why is Matplotlib still having these issues with unicode? I would not mind back in 90's but it is 2015 and not being able to use diacritics properly is pain.

Why do the “hooks” work under OS X, yet do not have any effect in openSUSE server?

matplotlib not finding the correct fonts can be caused by stale font cache (this is a problem we need to solve better).

To find were your cache is do

import matplotlib.font_manager as fm
print(fm.cachedir)  # or other way of getting the text out

And then delete the font-related contents in that directory. They will be rebuilt the next time you import matplotlib and (should) now contain the updated fonts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM