Clayton
Clayton

Reputation: 1545

Modifying number of ticks on Pandas hourly time axis

If I have the following example Python code using a Pandas dataframe:

import pandas as pd
from datetime import datetime

ts = pd.DataFrame(randn(1000), index=pd.date_range('1/1/2000 00:00:00', freq='H', periods=1000), columns=['Data'])
ts['Time'] = ts.index.map(lambda t: t.time())
ts = ts.groupby('Time').mean()
ts.plot(x_compat=True, figsize=(20,10))

The output plot is: enter image description here

What is the most elegant way to get the X-Axis ticks to automatically space themselves hourly or bi-hourly? x_compat=True has no impact

Upvotes: 2

Views: 3931

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58895

You can pass to ts.plot() the argument xticks. Giving the right interval you can plot hourly our bi-hourly like:

max_sec = 90000

ts.plot(x_compat=True, figsize=(20,10), xticks=arange(0, max_sec, 3600))

ts.plot(x_compat=True, figsize=(20,10), xticks=arange(0, max_sec, 7200))

Here max_sec is the maximum value of the xaxis, in seconds.

Upvotes: 3

Related Questions