5

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.

Shahriar.M
  • 818
  • 1
  • 11
  • 24

3 Answers3

3

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

0

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

Robo
  • 19
  • 5
  • 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](https://stackoverflow.com/help/referencing). – user11717481 Sep 16 '22 at 00:47
0

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

Persian LionKing
  • 304
  • 1
  • 5
  • 19