如何使x轴中的单词在matplotlib中从右向左显示?

2024-05-15 05:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here 我有一个元组列表。在每个元组中都有一个单词(波斯语)和一个表示频率的数字。当我运行代码进行绘图时,横轴中的单词似乎从左到右对齐,这是错误的顺序!我做了一些搜索,发现我需要使用“阿拉伯语_整形器”。我可以成功地重塑和对齐标签中的单词,但是,我找不到对x轴执行相同操作的方法

列表中的第三个单词(‘کلمات’,1046))与仅反转x轴的标签相同!有什么快速解决办法吗

from bidi.algorithm import get_display
import matplotlib.pyplot as plt
import arabic_reshaper

data = [('اندر', 1609), ('جان', 1296), ('کلمات', 1046), ('دل', 896)]


x = [x[0] for x in data]
y = [x[1] for x in data]


xlbl = get_display( arabic_reshaper.reshape('کلمات'))
ylbl = get_display( arabic_reshaper.reshape('فراوانی'))
plt.bar(x, y, label='Bar1', color='blue')
plt.xlabel(xlbl, fontdict=None, labelpad=None)
plt.ylabel(ylbl, fontdict=None, labelpad=None)
plt.show()

Tags: inimportnone列表fordatagetdisplay
1条回答
网友
1楼 · 发布于 2024-05-15 05:22:24

经过几个小时的搜索,我终于找到了答案

以下是新代码:

from bidi.algorithm import get_display
import matplotlib.pyplot as plt
import arabic_reshaper
from arabic_reshaper import reshape

data = [('اندر', 1609), ('جان', 1296), ('کلمات', 1046), ('دل', 896)]


x = [get_display(reshape(x[0])) for x in data] #pesky fix!
y = [x[1] for x in data]


print(x)
xlbl = get_display( arabic_reshaper.reshape('کلمات'))
ylbl = get_display( arabic_reshaper.reshape('فراوانی'))
plt.bar(x, y, label='Bar1', color='blue')
plt.xlabel(xlbl, fontdict=None, labelpad=None)
plt.ylabel(ylbl, fontdict=None, labelpad=None)
plt.savefig('pie.png', dpi=200)
plt.show()

enter image description here

相关问题 更多 >

    热门问题