Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing unicode characters, like করে, in matplotlib

I want to plot the text, "কৃষক জমিতে ধান চাষ করে", with matplotlib, what to do...?

I tried the flowing, but it didn't work.

s = u"কৃষক জমিতে ধান চাষ করে"
x = 0.2
y = 0.2
matplotlib.pyplot.text(x, y, s)
like image 209
Md. Zakir Hossan Avatar asked Apr 10 '17 04:04

Md. Zakir Hossan


1 Answers

Python3

This should work with a font that contains these unicode characters. I used kalpurush to cover the Bangla unicodes. You can pass the font to matplotlib like this:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

prop = fm.FontProperties(fname='kalpurush.ttf')
s = u"কৃষক জমিতে ধান চাষ করে"
x = 0.2
y = 0.2
plt.text(x, y, s, fontproperties=prop)
plt.show()

enter image description here

Bonus: Python2

For python 2 this is super easy. just add this at the top of the file:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
like image 140
lpoorthuis Avatar answered Oct 08 '22 11:10

lpoorthuis