Search code examples
pythonmatplotlibplotstockordinal

matplotlib - to show dates instead of ordinal number in the x-axis in my plot (stock price vs dates). I am using "toordina()l" to plot the graph


My plot shows on the x-axis the ordinal numbers (from dates) used a good fitting plot. But I want the axis to show dates in the normal format (dd--mm--yyyy) without changing the plot in any way.

Also,

ordinalDates = []

for the whole stock cvs:

          dateToNumber = datetime.datetime.toordinal(datetime.date())  
          ordinalDates.append(dateToNumber)

I have a list having all the converted dates (in ordinal number). and this list is the one I use to fit the linear regression curve as well as plot it.

Check Image


Solution

  • Convert them to a string in a second list:

    dateToNumber = datetime.datetime.toordinal(datetime.date())  
    ordinalDates.append(dateToNumber)
    strDates.append(str(datetime.date()))
    

    And then update the xticklabels:

    plt.set_xticklabels(strDates)