2
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.font_manager import FontProperties

hindi_font = FontProperties(fname = '/home/dip/WebScraping/fonts/Mangal.ttf')
figure(num=None, figsize=(15, 6), dpi=80, facecolor='w', edgecolor='k')
a = [('सरकारले', 3410), ('अर्ब', 3143), ('आर्थिक', 3026), ('रुपैयाँ', 2965),
     ('कारण', 2758), ('काम', 2745), ('व्यवस्था', 2683), ('समेत', 2580)]
b = dict(a)

val_val = b.values()
key_val = b.keys()
plt.bar(key_val,val_val)
plt.show()

Output Image Here But its not getting rendered as seen in the abouve screenshot text is displayed like rectangle boxes.

Dipendra Pant
  • 365
  • 3
  • 9

1 Answers1

1

This method allows you to plot directly using specific fonts. You can choose the font of your choice, as here the Mangal font is used according to your choice. All you have to do is apply the font properties to each label.

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from matplotlib.font_manager import FontProperties


font_prop = FontProperties(fname='Mangal.ttf', size=18)


figure(num=None, figsize=(15, 6), dpi=80, facecolor='w', edgecolor='k')
a = [('सरकारले', 3410), ('अर्ब', 3143), ('आर्थिक', 3026), ('रुपैयाँ', 2965),
     ('कारण', 2758), ('काम', 2745), ('व्यवस्था', 2683), ('समेत', 2580)]
b = dict(a)

ax = plt.subplot()

val_val = b.values()
key_val = b.keys()
br = plt.bar(key_val, val_val)


for label in (ax.get_xticklabels() + ax.get_yticklabels()):
    label.set_fontproperties(font_prop)
    label.set_fontsize(15)  # Size here overrides font_prop


plt.title("नेपाली भाषामा शीर्षक", fontproperties=font_prop,
          size=22, verticalalignment='bottom')  # Size here overrides font_prop
plt.xlabel("लेबलहरू", fontproperties=font_prop)
plt.ylabel("लेबलहरू", fontproperties=font_prop)


plt.show()

I also gave titles and labels using Google Translate. I don't know Nepali, apology if the translation is stupid.

python matplotlib bar with unicode

Arafat Hasan
  • 2,811
  • 3
  • 21
  • 38