LearningSlowly
LearningSlowly

Reputation: 9451

ggplot python handling time data over many weeks at hourly resolution

I am plotting journey time for a series of roads at hourly resolution, with data over a few weeks.

I can plot using unix time, but that isn't very intuitive. This is 7 days worth of data.

enter image description here

I used a function to manipulate the time field in order to give the date and hour:

def plot_time(time):
    return time.strftime('%Y-%m-%d-%H')

However, this results in ggplot throwing a value error when trying to plot:

ValueError: invalid literal for float(): 2016-04-13-00

Is there a simpler means of displaying date and some hour?

Alternatively I could plot unix time with a date scale on the axis but it would be nice to have some hour resolution on the axis.

Upvotes: 7

Views: 711

Answers (2)

LearningSlowly
LearningSlowly

Reputation: 9451

POSIXct was the key here as bVa said.

ggplot(aes(x=as.POSIXct(epoch_time,origin="1970-01-01"),y=lambda)

Once the epoch time was formated POSIXct, it was possible to use scale_x_datetime()

I settled on:

+ scale_x_datetime(breaks=date_breaks("1 day"))

Upvotes: 3

sashadereh
sashadereh

Reputation: 243

I think scale_date is what you exactly need.

Upvotes: 0

Related Questions