0

I have the following pandas dataframe, which consist of datetime timestamps and user ids:

  id        datetime
  130    2018-05-17 19:46:18
  133    2018-05-17 20:59:57
  131    2018-05-17 21:54:01
  142    2018-05-17 22:49:07
  114    2018-05-17 23:02:34
  136    2018-05-18 06:06:48
  324    2018-05-18 12:21:38
  180    2018-05-18 12:49:33
  120    2018-05-18 14:03:58
  120    2018-05-18 15:28:36

How can I plot on the y axis the id and on the x axis day or minutes? I tried to:

plt.plot(df3['datatime'], df3['id'], '|')
plt.xticks(rotation='vertical')

However, I have two problems, my dataframe is quite large and I have multiple ids, the second problem is that I wasn't able to arrange each label on the y axis and plot it against its datime value in the x axis. Any idea of how to do something like this:

example

The whole objective of this plot is to visualize the logins per time of that specific user.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
anon
  • 836
  • 2
  • 9
  • 25

1 Answers1

0

Something like this?

X axis: date, Y axis: id

from datetime import date
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd

# set your data as df
# strip only YYYY-mm-dd part from original `datetime` column
df.datetime = df.datetime.apply(lambda x: str(x)[:10])
df.datetime = df.datetime.apply(lambda x: date(int(x[:4]), int(x[5:7]), int(x[8:10])))

# plot
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(df.datetime, df.id, '|')
plt.gcf().autofmt_xdate()

Output:

enter image description here

gyoza
  • 2,112
  • 2
  • 12
  • 18
  • Hi thanks for the help, this was helpful, however, my pandas dataframe is very big, I have a lot of ids, is there any way of showing all this ids in an image properly? – anon Jun 23 '18 at 12:01