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.

2
  • You might need to show your desired output even if it means sketching out an image version. Commented Jun 22, 2018 at 19:51
  • I updated with a sample of the expected output @Parfait Commented Jun 22, 2018 at 20:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.