badc0re
badc0re

Reputation: 3523

Unicode character render

I can't find a way to render the unicode characters in the matlibplot chart:

 # -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from numpy.random import normal
from itertools import count
import matplotlib
a = {u'\u043f\u0430\u0440\u0435\u0430\u0442\u0430': 0.018144370928201092, u'\u0440\u0435\u043b\u0430\u0442\u0438\u0432\u0438\u0442\u0438\u0441\u0442\u0438\u0447\u043a\u0430\u0442\u0430': 0.008406336989671147, u'\u043d\u0430\u043e\u0453\u0430': 0.0704839024518581, u'\u0444\u0438\u0437\u0438\u043a\u0430': 0.025219010969013446, u'\u043a\u043e\u0438': 0.0039908251856718025, u'\u0438\u0437\u0431\u0443\u0432\u043d\u0430\u043b\u0430': 0.0035792606713834184, u'\u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430\u0442\u0430': 0.008406336989671147, u'\u043c\u0438\u043a\u0440\u043e\u0434\u0438\u043c\u0435\u043d\u0437\u0438\u0438': 0.008406336989671147, u'\u0434\u0430\u0432\u0430': 0.016812673979342295, u'\u0441\u0442\u0440\u0430\u043d\u0430': 0.008406336989671147}

plt.bar(*zip(*zip(count(), a.values())))
plt.title("Some chart")
plt.xticks(*zip(*zip(count(0.4), (a.keys()))))
plt.xlabel("Value")
plt.ylabel("Frequency")

plt.show()

Upvotes: 2

Views: 943

Answers (1)

unutbu
unutbu

Reputation: 879103

Specify a font with Cyrillic characters. For example,

import matplotlib as mpl
mpl.rcParams['font.family'] = 'Arial'

Your default font might be 'Helvetica', which does not have glyphs for the required characters.


import matplotlib.pyplot as plt
from numpy.random import normal
from itertools import count
import matplotlib as mpl
mpl.rcParams['font.family'] = 'Arial'
a = {u'\u043f\u0430\u0440\u0435\u0430\u0442\u0430': 0.018144370928201092, u'\u0440\u0435\u043b\u0430\u0442\u0438\u0432\u0438\u0442\u0438\u0441\u0442\u0438\u0447\u043a\u0430\u0442\u0430': 0.008406336989671147, u'\u043d\u0430\u043e\u0453\u0430': 0.0704839024518581, u'\u0444\u0438\u0437\u0438\u043a\u0430': 0.025219010969013446, u'\u043a\u043e\u0438': 0.0039908251856718025, u'\u0438\u0437\u0431\u0443\u0432\u043d\u0430\u043b\u0430': 0.0035792606713834184, u'\u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430\u0442\u0430': 0.008406336989671147, u'\u043c\u0438\u043a\u0440\u043e\u0434\u0438\u043c\u0435\u043d\u0437\u0438\u0438': 0.008406336989671147, u'\u0434\u0430\u0432\u0430': 0.016812673979342295, u'\u0441\u0442\u0440\u0430\u043d\u0430': 0.008406336989671147}

plt.bar(*zip(*zip(count(), a.values())))
plt.title("Gaussian Histogram")
plt.xticks(*zip(*zip(count(0.4), a.keys())))
plt.xlabel("Value")
plt.ylabel("Frequency")

plt.show()

enter image description here


If you don't have the Arial font, you can generate a list of font files known to your system with

import matplotlib.font_manager as font_manager
print(sorted(font_manager.findSystemFonts()))

You can then try out the fonts using

prop = font_manager.FontProperties(fname=path)
mpl.rcParams['font.family'] = prop.get_name()

where path is one of the paths returned by findSystemFonts().

Upvotes: 3

Related Questions