Is there a way to show Persian date in x-axis of Matplotlib plot
Asked Answered
H

3

6

I want to plot a dataframe in which the index consists of datetime values based on Iranian calendar.

I want to set the x-axis labels as following:

import matplotlib.dates as mdates

axes.scatter(df_day.index.values, df_day.loc[:, item], marker='.')
axes.xaxis.set_major_formatter(dates.DateFormatter('%d-%b %H:%M'))

Consider that I want to print the months's name in Persian instead of default value of Georgian calendar such as Jun or Aug.

I used jdatetime to convert the datetime in persian calendar but the plot function doesn't take it's value as x-axis index. then I used jalaali to create a datetime based on persian calendar that plot accepts it.

now the problem is how to print x-axis label as Persian month names? because I want this format:

dates.DateFormatter('%d-%b %H:%M')

Is there any way of doing that?

UPDATE

I used locale to access to the POSIX locale database and functionality as following:

import locale 

locale.setlocale(locale.LC_ALL, 'fa_IR')

but it only changed the name of Georgian months from English alphabet in Persian alphabet instead of showing jalali names such as فروردین or اردیبهشت.

any clue is appreciated.

Histo answered 11/8, 2020 at 10:57 Comment(0)
S
4

you can use plotly instead of matplotlib here is a simple example :

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()

output is like this figure: enter image description here

Sestos answered 11/9, 2021 at 10:34 Comment(0)
P
1

if you have a georgian dates column in your dataframe, you can simply use georgian dates in datetime.date format for your x-axis, and use this line of code to show them as persian dates.

fig.update_xaxes(calendar='jalali')

it doesn't need to use jdatetime.

your output would be like this: screenshot

Propagable answered 12/4, 2023 at 18:31 Comment(0)
I
0

My suggestion is to use django-jalali-date Here is the PyPI link to it. https://pypi.org/project/django-jalali-date/

Innkeeper answered 10/9, 2022 at 6:49 Comment(1)
Although the attached page contains a possible solution to the question, it is better to add detailed information such as citations or code snippets that point to a given answer, it is also recommended to read How to reference material written by others.Ogburn

© 2022 - 2024 — McMap. All rights reserved.