pythonnoob
pythonnoob

Reputation: 49

How to plot date on y-axis and hours on x-axis?

enter image description here

Hello !

I have discrete list of : found_time = ['2019-02-28 00:24:16', '2019-02-28 00:22:30', '2019-02-28 00:20:16', '2019-02-28 00:19:25', '2019-02-28 00:18:11', '2019-02-27 23:09:45', '2019-02-27 22:52:09', '2019-02-27 22:50:05', '2019-02-27 22:44:31', '2019-02-27 18:04:18', '2019-02-27 08:08:21', ... ... , '2019-02-01 22:21:10', '2019-02-01 00:21:10']

and I want to plot dots as the linked image by using matplotlib or something.

I managed to separate date and time by using

data = panda.to_datetime(found_time, yearfirst=True)
x = data.time
y = data.date

and I tried plt.xlim(["00:00:00", "23:59:59"]), but the interval was random.

Question: How to set y-axis 02-01 ~ 02-28 with interval of 1 day, and x-axis 00:00 ~ 24:00(or 23:59) with interval of 1 hour?

Upvotes: 2

Views: 818

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

You could try first turning your list of datetimes into a pandas.Series, then resample to hour. Something like:

import pandas as pd
import numpy as np

s = pd.Series(np.ones(len(found_time)), index=pd.DatetimeIndex(found_time))
s = s.resample('H').sum()

plt.scatter(s.index.hour, s.index.date, s=s, color='k')

# yticks
yticks = pd.date_range('2019-02-01', '2019-02-28', freq='D')
plt.yticks(yticks, [y.strftime('%m-%d') for y in yticks])
plt.ylim('2019-02-01', '2019-02-28')

# xticks
xticks = np.arange(24)
plt.xticks(xticks, ['{0:02d}:00'.format(x) for x in xticks], rotation=45, ha='right')

Upvotes: 2

Related Questions