Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing xticks to the name of the months

i have a multilevel index series whose outer level index is month and inner level index is day let say it is stored in series

print(series)

Please see the output->output

So to make plot i use the below code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

series.plot.line(figsize=(20,10))

i am getting
please see the output->
output

but i want xticks to be [January, February,……,December] instead of those tuple values how can i do that???

>Solution :

If need only months names is possible remove days from Multiindex by Series.droplevel and convert to datetimes with DatetimeIndex.month_name:

series.index = pd.to_datetime(series.index.droplevel(1), format='%m').month_name()
series.plot.line(figsize=(20,10))

Or if need more complex format, e.g. days with month names use matplotlib.dates.DateFormatter with convert MulitIndex to datetimes:

from matplotlib.dates import DateFormatter

series.index = pd.to_datetime([f'2023-{m}-{d}' for m, d in series.index])
    
ax = series.plot.line(figsize=(20,10))

date_form = DateFormatter("%d-%B")
ax.xaxis.set_major_formatter(date_form)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading