0

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()
Community
  • 1
  • 1
  • 1
    You need to show us your code... Or just google matlab plot – ford prefect Dec 16 '13 at 16:24
  • 1
    @inquisitiveIdiot note this is a matplotlib/python, not MATLAB question. – tacaswell Dec 16 '13 at 18:28
  • What have you tried? You will get much better help _fixing_ broken code than asking us to write your code for you. You just need to convert your time stamps to `datetime` objects, then drop the date part. You should be able to directly plot the time vs your y values. – tacaswell Dec 16 '13 at 18:30

2 Answers2

1

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()
Paul H
  • 65,268
  • 20
  • 159
  • 136
0

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()
Troy Rockwood
  • 5,875
  • 3
  • 15
  • 20