5

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)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Md. Zakir Hossan
  • 517
  • 2
  • 6
  • 17

1 Answers1

6

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 -*-
lpoorthuis
  • 111
  • 4
  • are you sure that your font covers the unicode characters? – lpoorthuis Apr 20 '17 at 10:50
  • #!/usr/bin/env python # -*- coding: utf-8 -*- 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() – Md. Zakir Hossan Apr 20 '17 at 11:37
  • 1
    the header is only relevant if you use python2. the path to the `ttf` file must be correct. in your case the file has to be where your source code is – lpoorthuis Apr 20 '17 at 12:15
  • the ttf file is in where my source code is, and I put the header how you wrote. – Md. Zakir Hossan Apr 20 '17 at 12:23
  • 1
    I could get the Bengali font as shown in the figure. However, if you notice, the fonts are not in the correct order. "জমিতে" and "করে" are not plotted correctly. Do you have any suggestions? – Raj Sep 17 '19 at 20:49