2

I am trying to get a dataframe column to display the date as example "Jan 15, Feb 15 etc" on a chart. But it was displaying as "Feb 115, Mar 115 etc" I have tried to adapt some code using matplotlib.ticker. but then the chart comes out with no axis. Here is a simplified version of the code, not working.

import matplotlib.pyplot as plt
import matplotlib.ticker as tkr


# create figure instance
fig1 = plt.figure(1)

ax = fig1.add_subplot(2,1,1)

x = 41640, 41671, 41699, 41730, 41760, 41791

y = 1, 4, 7, 9, 15, 18

ax.get_xaxis().set_major_formatter(
    tkr.FuncFormatter(lambda x, p: format((x), '%b %y')))


ax.plot_date(x, y)
fig1.show()
3
  • 2
    Hah! 115 is the year if you interpret your numbers as Julian date. Are your numbers modified julian date (MJD), something else? In any case, the standard python format() assumes as many format pieces as inputs; you probably wanted something from matplotlib.dates or datetime. Commented Sep 22, 2014 at 15:13
  • @mdurant, not sure. I was importing dates from MS Excel, i tried pre-working the data into different formats but got the same results. I mean if i load the date format MMM-YY for example can matplotlib work with that or not? or even if i load 01/12/2014 or any format. Is it a probably because of US and UK date formats. Commented Sep 23, 2014 at 7:33
  • Do you know what the dates should be? If they are MJD, you need to add 2400000 to convert to JD and matplotlib.dates.julian2num (or subtract 1721424) to convert to "proleptic Gregorian", which is what you need. Commented Sep 23, 2014 at 14:35

1 Answer 1

3

Here is some working code:

import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import matplotlib.dates as mdates
import datetime

# create figure instance
fig1 = plt.figure(1)
ax = fig1.add_subplot(2,1,1)

start_date = datetime.date(1899, 12, 30)
x = 41640, 41671, 41699, 41730, 41760, 41791
dates = [start_date + datetime.timedelta(xval) for xval in x]
y = 1, 4, 7, 9, 15, 18

ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator(bymonthday=(1,15)))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
plt.plot(dates, y, 'o')
plt.show()

Explanation:

It turns out serial dates in MS Excel start from Dec 30, 1899. i.e. number 0 is Dec 30, 1899, and 1 corresponds to Dec 31, 1899 (try it out in Excel yourself ^_^). Although the Excel help says that "January 1, 1900 is serial number 1", it is incorrect, since there was an early bug that seems to be fixed.

To convert the Excel serial dates to a Python datetime object, checkout datetime.timedelta.

To plot datetime in matplotlib, checkout its demo and API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.