绘制最常用的 20 个词

plot most 20 frequent words

我在这里尝试使用 plot 设置最常用的单词,但我遇到了一个问题,因为语言是阿拉伯语并且它与格式不匹配

fig, ax = plt.subplots(figsize=(12, 10))
sns.barplot(x="word", y="freq", data=word_counter_df, palette="PuBuGn_d", ax=ax)
plt.show();

我试过 ast 解码,但它与 plot 不匹配

import ast
fig, ax = plt.subplots(figsize=(12, 10))
sns.barplot(x="word", y="freq", data=word_counter_df.apply(ast.literal_eval).str.decode("utf-8"), palette="PuBuGn_d", ax=ax)
plt.show();

word_counter_df 看起来像:

<class 'pandas.core.frame.DataFrame'>
      word  freq
0   الله    6829
1   علي     5636
2   ان      3732
3   اللهم   2575
4   انا     2436
5   صباح    2115
6   اللي    1792
7   الي     1709
8   والله   1645
9   الهلال  1520
10  الا     1394
11  الخير   1276
12  انت     1209
13  يارب    1089
14  يوم     1082
15  رتويت   1019
16  كان     1004
17  اذا     994 
18  لله     982 
19  اي      939 

它返回带有此错误的空图:

ValueError: ('malformed node or string: 0 الله \n1 علي \n2 ان \n3 اللهم \n4 انا \n5 صباح \n6 اللي \n7
الي \n8 والله \n9 الهلال\n10 الا \n11 الخير \n12
انت \n13 يارب \n14 يوم \n15 رتويت \n16 كان \n17
اذا \n18 لله \n19 اي \nName: word, dtype: object', 'occurred at index word')

您可以使用 pandas' 内置 plot.bar 函数:

word_counter_df.plot.bar(x="word", y="freq")
plt.show()



更新连接的阿拉伯字母

import arabic_reshaper
from bidi.algorithm import get_display
word_counter_df['disp'] = word_counter_df.word.apply(arabic_reshaper.reshape).apply(get_display)
word_counter_df.plot.bar(x="disp", y="freq")

与seaborn相同(版本0.9.0)here。