user3107878
user3107878

Reputation: 1

matplotlib: Plot graph using time and ignoring year/month/date

I am a novice to Python/matplotlib so please bear with me! I have a list of epoch timestamps across several days and a boolean indicating whether an event occurred or not. I want to plot this using matplotlib with time on x-axis and Y-axis showing 1/0 and I see SO examples Plotting time in Python with Matplotlib for doing this.

However, I want to ignore the yr/month/date and plot only again time, i.e. 8 AM time on 1-Dec and 8 AM time on 2-Dec use the same X-axis coordinate.

Edit: Here is the current code:

import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

event = [
(1384528771000000000, 1),
(1384550132000000000, 0),
(1384881104000000000, 0),
(1384962750000000000, 1),
(1384966615000000000, 1),
(1385049149000000000, 1),
(1385053051000000000, 0),
(1385053939000000000, 0),
(1385140573000000000, 1),
(1385393839000000000, 1),
(1385398965000000000, 0),
(1385410739000000000, 1),
(1385483309000000000, 1),
(1385587272000000000, 0),
(1385998456000000000, 1),
(1386084047000000000, 0),
(1386085865000000000, 1),
(1386259016000000000, 0),
(1386345606000000000, 0),
(1386602368000000000, 1)
]

for line in event:
    timeStmp = datetime.datetime.strptime(time.ctime(line[0]/1000000000), "%a %b %d %H:%M:%S %Y")
    print timeStmp, mdates.date2num(timeStmp)
    plt.plot_date(mdates.date2num(timeStmp),line[1])
plt.show()

Upvotes: 0

Views: 2629

Answers (2)

Troy Rockwood
Troy Rockwood

Reputation: 6147

Here is one way to do this, just plot the time difference between the beginning of the day and the time in the timestamp:

import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib

event = [
(1384528771000000000, 1),
(1384550132000000000, 0),
(1384881104000000000, 0),
(1384962750000000000, 1),
(1384966615000000000, 1),
(1385049149000000000, 1),
(1385053051000000000, 0),
(1385053939000000000, 0),
(1385140573000000000, 1),
(1385393839000000000, 1),
(1385398965000000000, 0),
(1385410739000000000, 1),
(1385483309000000000, 1),
(1385587272000000000, 0),
(1385998456000000000, 1),
(1386084047000000000, 0),
(1386085865000000000, 1),
(1386259016000000000, 0),
(1386345606000000000, 0),
(1386602368000000000, 1)
]

fig = plt.figure()
ax = fig.add_subplot(111)

for line in event:
    timeStmp = datetime.datetime.strptime(time.ctime(line[0]/1000000000), "%a %b %d %H:%M:%S %Y")
    print timeStmp, mdates.date2num(timeStmp)
    tdelta = timeStmp - datetime.datetime.strptime(str(timeStmp.year) + " " + str(timeStmp.month) + " " + str(timeStmp.day) + " 00:00:00", "%Y %m %d %H:%M:%S")
    ax.plot(tdelta.total_seconds(),line[1], 'o')

def timeTicks(x, pos):
    d = datetime.timedelta(seconds=x)
    return str(d)
formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
plt.show()

Upvotes: 0

Paul H
Paul H

Reputation: 68186

So the only thing I'm doing differently here, is taking your date/time stamps, converting to a string of just time data, then using mdates to convert that time string to a number. Crude and inefficient, but I think it's what you want.

for line in event:
    datetimeStamp = datetime.datetime.strptime(time.ctime(line[0]/1000000000), "%a %b %d %H:%M:%S %Y")
    timeStamp = datetimeStamp.strftime('%H:%M:%S')

    print(timeStamp, mdates.datestr2num(timeStamp))
    plt.plot_date(mdates.datestr2num(timeStamp),line[1])
plt.show()

Upvotes: 1

Related Questions