Vatsal Shah
Vatsal Shah

Reputation: 21

utf-8 character printing in matplotlib or seaborn

I have utf-8(hindi) word vectors and want to visualize. it using t-SNE and I am using annotate method of matplotlib to plot words.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1,2]
y = [4,2]
hindi_word = [u'के', u'है']
for i, txt in enumerate(hindi_word):
    ax.annotate(txt, (x[i],y[i]))

ax.scatter(x,y)

It shows boxes instead of actual words 'के', 'है' as attached in below image. How to print actual utf-8 words instead of boxes.

output image

Upvotes: 2

Views: 2494

Answers (1)

Daan
Daan

Reputation: 942

You need to change the font of the typesetting. The standard font does not support your desired characters. I have found a found in matplotlib that does the trick: Lohit Devanagari. I'm not sure that this is the only possibility, this was the first that worked.

Before annotating, add this line to change the font:

plt.rc('font', family='Lohit Devanagari')

Matplotlib now renders the correct characters:

output

Upvotes: 2

Related Questions