【发布时间】:2020-03-17 15:55:36
【问题描述】:
我想在 matplotlib 中使用波斯字体,但是当像 فایل pdf 那样混合文本时,英文文本会显示为一些正方形。我的代码如下:
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()
【问题讨论】:
-
问题是字体文件
BNasanin.ttf不支持英文字符-大概是因为它是纯波斯语字体文件。解决方案是使用两种不同的字体文件或同时支持波斯语和英文字符的单个文件。在 Matplotlib 文本实例中混合字体可能只能使用 LaTex,我不确定
标签: python matplotlib