有没有办法在 Matplotlib 图的 x 轴上显示波斯日期

Sha*_*r.M 6 python datetime matplotlib pandas persian-calendar

我想绘制一个dataframe,其中由基于伊朗日历index的值组成。datetime

\n

我想将x-axis标签设置如下:

\n
import matplotlib.dates as mdates\n\naxes.scatter(df_day.index.values, df_day.loc[:, item], marker=\'.\')\naxes.xaxis.set_major_formatter(dates.DateFormatter(\'%d-%b %H:%M\'))\n
Run Code Online (Sandbox Code Playgroud)\n

考虑到我想用波斯语打印月份的名称,而不是Georgian日历的默认值,例如 JunAug

\n

我使用jdatetime来转换datetime波斯日历,但该plot函数不将其值作为x-axis索引。然后我使用jalaali创建了一个datetime基于波斯日历的情节,该日历接受它。

\n

现在的问题是如何将x-axis标签打印为波斯月份名称?因为我想要这种格式:

\n
dates.DateFormatter(\'%d-%b %H:%M\')\n
Run Code Online (Sandbox Code Playgroud)\n

有什么办法可以做到这一点吗?

\n

更新

\n

我使用区域设置来访问 POSIX 区域设置数据库和功能,如下所示:

\n
import locale \n\nlocale.setlocale(locale.LC_ALL, \'fa_IR\')\n
Run Code Online (Sandbox Code Playgroud)\n

但它只是改变了字母表中Georgian月份的名称,而不是显示诸如或 之类的名称。EnglishPersianjalali\xd9\x81\xd8\xb1\xd9\x88\xd8\xb1\xd8\xaf\xdb\x8c\xd9\x86\xd8\xa7\xd8\xb1\xd8\xaf\xdb\x8c\xd8\xa8\xd9\x87\xd8\xb4\xd8\xaa

\n

任何线索表示赞赏。

\n

小智 4

你可以使用plotly而不是matplotlib这里是一个简单的例子:

import datetime
import plotly.express as px

base = datetime.datetime.today()
date_list = [base + datetime.timedelta(days=x) for x in range(200)]
nums = list(range(200))

fig = px.line(x=date_list, y=nums)
fig.update_xaxes(calendar='jalali')
fig.show()
Run Code Online (Sandbox Code Playgroud)

输出如下图所示: 在此输入图像描述