python - 用阿拉伯字符绘制直方图

标签 python matplotlib character-encoding seaborn arabic

我正在尝试绘制以 arabic 书写的最常用单词的直方图,但我想不出办法做到这一点。我所能得到的只是切片字符,而不是编译后的单词。

这是我得到的一个例子:

enter image description here

import seaborn as sns

import pandas as pd

res = {
 'الذكاء': 8,
 'الاصطناعي': 9,
 'هو': 2,
 'سلوك': 1,
 'وخصائص': 1,
 'معينة': 1,
 'تتسم': 1
}

df = pd.DataFrame(res.items(), columns=['word', 'count'])

sns.set(style="whitegrid")
ax = sns.barplot(x="count", y="word", data=df)

如上图所示,我希望对这些字符进行编译,就像字典中提到的那样。

最佳答案

这似乎与 arabic_reshaper 运行良好和 bidi正如@Sheldore 所指出的。

import seaborn as sns
import pandas as pd
import arabic_reshaper
from bidi.algorithm import get_display

res = {
 'الذكاء': 8,
 'الاصطناعي': 9,
 'هو': 2,
 'سلوك': 1,
 'وخصائص': 1,
 'معينة': 1,
 'تتسم': 1
}

res2 = {get_display(arabic_reshaper.reshape(k)): v for k,v in res.items()}

df = pd.DataFrame(res2.items(), columns=['word', 'count'])

sns.set(style="whitegrid")
ax = sns.barplot(x="count", y="word", data=df)
barplot with arabic labels

关于python - 用阿拉伯字符绘制直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56292850/

相关文章:

python - python中的标准输出编码

python - AttributeError - 即使似乎没有属性错误

python - 对字典中具有相同键的值求和

datetime - "ValueError: year is out of range"尝试使用 matplotlib pyplot 时

Python Matplotlib 在一张图片中制作动画条形图和绘图

character-encoding - 人名的所有允许字符是什么?

python/clojure 相当于 mathematica 的 NestWhileList

python - Theano 安装,nvcc 不在路径中

python - 如何获取 matplotlib 中的像素颜色?

c# - 如何获得正确的字符串文本?