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()
3
  • 1
    You need to show us your code... Or just google matlab plot Commented Dec 16, 2013 at 16:24
  • 1
    @inquisitiveIdiot note this is a matplotlib/python, not MATLAB question. Commented Dec 16, 2013 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. Commented Dec 16, 2013 at 18:30

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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()

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.