1

I have this dataframe

    user_date   user_time
0   Thu         07:32:20
1   Wed         21:10:38
2   Fri         21:32:55
3   Sat         11:57:36
4   Fri         22:37:41

How to plot this dataframe with x is user_time with range 24hours and y is user_date.

In the meantime, thank you so much for your attention and participation.

Paldro
  • 71
  • 1
  • 2
  • 7

1 Answers1

1

There is problem with user_date values, because if use strings get:

TypeError: Empty 'DataFrame': no numeric data to plot

One possible solution is convert them to ordered categorical and plot by cat.codes.

Also plot timedelata is not implemented, so user_time was converted to to_datetime and then to strings by strftime:

days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
df['user_date'] = df['user_date'].astype('category', categories=days, ordered=True)

df['user_time1'] = pd.to_datetime(df['user_time']).dt.strftime('%H:%M:%S')
df['user_date1'] = df['user_date'].cat.codes

ax = df.plot(x='user_time1', y='user_date1')

#set max and min values of axis
#https://stackoverflow.com/a/43460067/2901002
ax.set_yticks(range(7))
ax.set_yticklabels(days)

graph

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Hi @jezrael, thanks for your help. I had this error: NameError: name 'ticker' is not defined The plot missing some data in dataframe. How to keep the user_time? I don't want to change to second. – Paldro Jun 23 '17 at 07:36
  • Yes, it was not easy, but now it should work. Please check solution. – jezrael Jun 23 '17 at 07:48
  • Hi, I had this error : ** _astype() got an unexpected keyword argument 'ors' **. I using Python 3, but after removing `ors=True, categories =days` everything ran fine! Thanks so much @jezrael – Paldro Jun 23 '17 at 08:09
  • Do you think axis x? from 0 to 24? – jezrael Jun 23 '17 at 08:21
  • I mean, I hope you can help me show all data of user_time. If I have 50 values of user_time, how to show all 50 value of that user on the plot ? – Paldro Jun 23 '17 at 08:29
  • I think I get it - use `ax = df.plot(x='user_time1', y='user_date1', style=".")` – jezrael Jun 23 '17 at 08:36
  • Thank you, idea is from [this answer](https://stackoverflow.com/a/29737663/2901002) – jezrael Jun 23 '17 at 08:38