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

How to set customizing xtick in matplotlib

I am using matplotlib to draw a plot chart as shown below.
The x-axis is dates.
I would like to display daily data in yyyy-mm-dd format as m in monthly units.
And I want the first date of each year to be displayed as yy.m, not m.
The first date of the year may not be January. Depending on the data, it could be February or October.

The example below is 2022-11-1. In this case, I would like to display it as 22.1.

I tried my best to write the code below, but it’s still not enough.

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

I’ve seen similar questions and answers before, but I couldn’t find an answer about labeling yy.m for the first date by year among randomly set dates.
​


import pandas as pd
import io
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import FuncFormatter

temp = u"""
date,yield
2022-11-01,4.0419 \n 2022-11-02,4.1005
\n 2022-11-03,4.1469\n 2022-11-04,4.1584\n 2022-11-05,4.1584\n 2022-11-06,4.1584\n 2022-11-07,4.2135\n 2022-11-08,4.1234\n 2022-11-09,4.0923\n 2022-11-10,3.8125\n 2022-11-11,3.8125\n 2022-11-12,3.8125\n 2022-11-13,3.8125\n 2022-11-14,3.8536
\n 2022-11-15,3.7696\n 2022-11-16,3.6899\n 2022-11-17,3.7657\n 2022-11-18,3.8288\n 2022-11-19,3.8288\n 2022-11-20,3.8288\n 2022-11-21,3.8269\n 2022-11-22,3.7559\n 2022-11-23,3.6927\n 2022-11-24,3.6927\n 2022-11-25,3.6776\n 2022-11-26,3.6776
\n 2022-11-27,3.6776\n 2022-11-28,3.6812\n 2022-11-29,3.7441\n 2022-11-30,3.6054\n 2022-12-01,3.5048\n 2022-12-02,3.4862\n 2022-12-03,3.4862\n 2022-12-04,3.4862\n 2022-12-05,3.5736\n 2022-12-06,3.5314\n 2022-12-07,3.4169\n 2022-12-08,3.4819
\n 2022-12-09,3.5783\n 2022-12-10,3.5783\n 2022-12-11,3.5783\n 2022-12-12,3.6113\n 2022-12-13,3.5012\n 2022-12-14,3.4774\n 2022-12-15,3.4463\n 2022-12-16,3.4822\n 2022-12-17,3.4822\n 2022-12-18,3.4822\n 2022-12-19,3.5846\n 2022-12-20,3.6825
\n 2022-12-21,3.662 \n 2022-12-22,3.6786\n 2022-12-23,3.7472\n 2022-12-24,3.7472\n 2022-12-25,3.7472\n 2022-12-26,3.7472\n 2022-12-27,3.8411\n 2022-12-28,3.8827\n 2022-12-29,3.8145\n 2022-12-30,3.8748\n 2022-12-31,3.8748\n 2023-01-01,3.8748
\n 2023-01-02,3.8748\n 2023-01-03,3.7389\n 2023-01-04,3.6827\n 2023-01-05,3.7181\n 2023-01-06,3.558\n 2023-01-07,3.558\n 2023-01-08,3.558\n 2023-01-09,3.5321\n 2023-01-10,3.6188\n 2023-01-11,3.5392\n 2023-01-12,3.44\n 2023-01-13,3.5035\n 2023-01-14,3.5035\n 2023-01-15,3.5035
\n 2023-01-16,3.5035\n 2023-01-17,3.5476\n 2023-01-18,3.3698\n 2023-01-19,3.3915\n 2023-01-20,3.4787\n 2023-01-21,3.4787\n 2023-01-22,3.4787\n 2023-01-23,3.5098\n 2023-01-24,3.4527\n 2023-01-25,3.4416\n 2023-01-26,3.4947\n 2023-01-27,3.5035\n 2023-01-28,3.5035\n 2023-01-29,3.5035
\n 2023-01-30,3.5366\n 2023-01-31,3.5069\n 2023-02-01,3.4166\n 2023-02-02,3.3927\n 2023-02-03,3.5246\n 2023-02-04,3.5246\n 2023-02-05,3.5246\n 2023-02-06,3.6399\n 2023-02-07,3.6735\n 2023-02-08,3.6098\n 2023-02-09,3.6579\n 2023-02-10,3.732
\n 2023-02-11,3.732\n 2023-02-12,3.732\n 2023-02-13,3.7016\n 2023-02-14,3.7435\n 2023-02-15,3.8049\n 2023-02-16,3.8608\n 2023-02-17,3.8148\n 2023-02-18,3.8148\n 2023-02-19,3.8148\n 2023-02-20,3.8148\n 2023-02-21,3.9525\n 2023-02-22,3.9156\n 2023-02-23,3.8768\n 2023-02-24,3.9432\n 2023-02-25,3.9432
\n 2023-02-26,3.9432\n 2023-02-27,3.9141\n 2023-02-28,3.92\n 2023-03-01,3.9925\n 2023-03-02,4.0556\n 2023-03-03,3.9517\n 2023-03-04,3.9517\n 2023-03-05,3.9517\n 2023-03-06,3.9577\n 2023-03-07,3.9637\n 2023-03-08,3.9913\n 2023-03-09,3.9032\n 2023-03-10,3.6987\n 2023-03-11,3.6987\n 2023-03-12,3.6987\n 2023-03-13,3.5732\n 2023-03-14,3.6892\n 2023-03-15,3.4548\n 2023-03-16,3.577
\n 2023-03-17,3.4286\n 2023-03-18,3.4286\n 2023-03-19,3.4286\n 2023-03-20,3.4847\n 2023-03-21,3.6094\n 2023-03-22,3.4341\n 2023-03-23,3.4266\n 2023-03-24,3.3762\n 2023-03-25,3.3762\n 2023-03-26,3.3762\n 2023-03-27,3.5299\n 2023-03-28,3.5696\n 2023-03-29,3.5639\n 2023-03-30,3.5488
\n 2023-03-31,3.4676\n 2023-04-01,3.4676\n 2023-04-02,3.4676\n 2023-04-03,3.4114\n 2023-04-04,3.3387\n 2023-04-05,3.3108\n 2023-04-06,3.305\n 2023-04-07,3.3906\n 2023-04-08,3.3906\n 2023-04-09,3.3906\n 2023-04-10,3.4168\n 2023-04-11,3.4262\n 2023-04-12,3.3906\n 2023-04-13,3.4449
\n 2023-04-14,3.5128\n 2023-04-15,3.5128\n 2023-04-16,3.5128\n 2023-04-17,3.6004\n 2023-04-18,3.5756\n 2023-04-19,3.5908\n 2023-04-20,3.5318\n 2023-04-21,3.5718\n 2023-04-22,3.5718\n 2023-04-23,3.5718\n 2023-04-24,3.4901\n 2023-04-25,3.3996\n 2023-04-26,3.4485\n 2023-04-27,3.5204\n 2023-04-28,3.422
\n 2023-04-29,3.422\n 2023-04-30,3.422\n 2023-05-01,3.5681\n 2023-05-02,3.4239\n 2023-05-03,3.3356\n 2023-05-04,3.3787\n 2023-05-05,3.437\n 2023-05-06,3.437\n 2023-05-07,3.437\n 2023-05-08,3.5072\n 2023-05-09,3.5186\n 2023-05-10,3.4426\n 2023-05-11,3.3843\n 2023-05-12,3.4625\n 2023-05-13,3.4625
\n 2023-05-14,3.4625\n 2023-05-15,3.5019\n 2023-05-16,3.5339\n 2023-05-17,3.5641\n 2023-05-18,3.6457\n 2023-05-19,3.6726\n 2023-05-20,3.6726\n 2023-05-21,3.6726\n 2023-05-22,3.7148\n 2023-05-23,3.6919
\n 2023-05-24,3.7419\n 2023-05-25,3.8174\n 2023-05-26,3.7983\n 2023-05-27,3.7983\n 2023-05-28,3.7983\n 2023-05-29,3.7983\n 2023-05-30,3.6866\n 2023-05-31,3.6426\n 2023-06-01,3.595\n 2023-06-02,3.6907\n 2023-06-03,3.6907\n 2023-06-04,3.6907\n 2023-06-05,3.6831

"""

temp = pd.read_csv(io.StringIO(temp), sep=",", parse_dates=False)
temp.date = pd.to_datetime(temp.date)


plt.plot(temp['date'], temp['yield'])

def custom_date_format(x, pos):
    date = mdates.num2date(x)
    if date.month == 1 and date.day == 1:
        return date.strftime('%y.%m')
    else:
        return date.strftime('%m')
    

ax = plt.gca()
ax.xaxis.set_major_locator(mdates.MonthLocator())
date_form = FuncFormatter(custom_date_format)
ax.xaxis.set_major_formatter(date_form)
plt.grid(True)
plt.xticks(rotation = 45)

enter image description here

>Solution :

I would suggest using pos to identify the position of the label on the graph, and using the full month/year formatting if this is the first label on the graph.

Example:

def custom_date_format(x, pos):
    date = mdates.num2date(x)
    if (date.month == 1 and date.day == 1) \
            or pos == 0:
        return date.strftime('%y.%m')
    else:
        return date.strftime('%m')

Output:

altered graph

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