2

I want to use persian fonts in matplotlib but when text is mixed like فایل pdf english text show as some square. My code in the following:

import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
from bidi.algorithm import get_display
import arabic_reshaper

from matplotlib import font_manager as fm, rcParams
import os

fpath = os.path.join(rcParams["datapath"],"C:\\Users\\Ahmad\\Desktop\\Report_Examples\\BNazanin.ttf")

prop = fm.FontProperties(fname=fpath)



def make_farsi_text(x):
    reshaped_text = arabic_reshaper.reshape(x)
    farsi_text = get_display(reshaped_text)
    return farsi_text

fmri = sns.load_dataset("fmri")
ax = sns.lineplot(x="timepoint", y="signal", data=fmri)
ax.set_xlabel(make_farsi_text('فایل pdf'),fontproperties=prop)
plt.show()

Result image

Ahmad
  • 906
  • 11
  • 27
  • 1
    The problem is that the font file `BNasanin.ttf` does not support the English characters - presumably because it is a pure Farsi font file. The solution would be to use two different font files or a single file which simultaneously supports Farsi and English characters. Mixing fonts in a Matplotlib text instance might only be possible with LaTex, I'm not sure – William Miller Mar 18 '20 at 07:08
  • 1
    [XB Niloofar download link](https://fontlibrary.org/en/font/xb-niloofar) – Trenton McKinney May 16 '21 at 18:05

1 Answers1

1

I use another font and my problem solved. I use XB Niloofar font. You can download it from following link:

XB Niloofar download link

Ahmad
  • 906
  • 11
  • 27