2

I have a dataframe that looks like this:

Time                                  Instances
00:32:00                              1
00:35:00                              1
00:51:00                              1
01:51:00                              1
04:08:00                              1
05:13:00                              1
05:14:00                              1
05:34:00                              1
05:51:00                              1
07:36:00                              1
07:53:00                              1
08:20:00                              1
09:34:00                              1
10:01:00                              1
10:18:00                              1
10:38:00                              1
10:51:00                              1
11:15:00                              1
12:14:00                              1
13:16:00                              1
13:28:00                              1
13:51:00                              1
15:56:00                              1
16:22:00                              1
16:35:00                              1
17:19:00                              1

I want to plot this but I would like the "ticks" on the x-axis to be 00:00:00,04:00:00,08:00:00,12:00:00,16:00:00,20:00:00,23:59:00. So I started out with this code('t' is the name of my data frame).

EDIT: Adding part of code that loads data in.

import pandas as pd
from datetime import date, timedelta
import numpy as np
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from matplotlib import cm
import random
import datetime
df=pd.read_csv(path)
df['Time']= pd.to_datetime(df['Time'])
df=df.sort_values("Time")
df.Time = df.Time.map(lambda x: x.replace(second=0))
df['Time'] =df['Time'].dt.time
t=df['Time'].value_counts().reset_index()
t.columns=['Time','Instances']
t=t.sort_values("Time")
ax = t.plot(x='Time',y='Instances',kind='bar', title ="Number of Instances By Time of Day", figsize=(20, 10), legend=True, fontsize=12)
ax.set_xticks([datetime.time(0,0,0,0),datetime.time(4,0,0,0),datetime.time(8,0,0,0),datetime.time(12,0,0,0),datetime.time(16,0,0,0),datetime.time(20,0,0,0),datetime.time(23,59,0,0)])
ax.set_xticklabels([datetime.time(0,0,0,0),datetime.time(4,0,0,0),datetime.time(8,0,0,0),datetime.time(12,0,0,0),datetime.time(16,0,0,0),datetime.time(20,0,0,0),datetime.time(23,59,0,0)])

When I run this code, however, I get a blank graph. enter image description here

Looking to fix this and any help would be appreciated!

AJHello
  • 115
  • 9

1 Answers1

0

I think the problem is with aliasing as I don't see what 't' is aliased as?.

ax = t.plot(x='Time',y='Instances',kind='bar', title ="Number of Instances By Time of Day", figsize=(20, 10), legend=True, fontsize=12)

Instead try

ax = plt.plot(x='Time',y='Instances',kind='bar', title ="Number of Instances By Time of Day", figsize=(20, 10), legend=True, fontsize=12)
Nikaido
  • 4,443
  • 5
  • 30
  • 47
Codewizard_26
  • 68
  • 1
  • 9
  • When I did plt.plot(x=t['Time'],y=t['Instances'],kind='bar',title='Number of Instances By Time of Day',figsize=(20,10),legend=True,fontsize=12) I got the error: "AttributeError: 'list' object has no attribute 'set_xticks'" on the set_xticks line of code. – AJHello Feb 13 '20 at 18:10
  • So why you are using x=t[time] ? What is the use of putting it in the list – Codewizard_26 Feb 13 '20 at 18:36
  • 1
    That's not where I am getting the error. The error is on the next line of code when I do ax.set_xticks([datetime.time(0,0,0,0),datetime.time(4,0,0,0),datetime.time(8,0,0,0),datetime.time(12,0,0,0),datetime.time(16,0,0,0),datetime.time(20,0,0,0),datetime.time(23,59,0,0)]). Even if I do not do t['Time'] I still get that same error. – AJHello Feb 13 '20 at 18:41
  • One last thing you can try is first try to run it by changing ax = t.plot(x='Time',y='Instances',kind='bar', title ="Number of Instances By Time of Day", figsize=(20, 10), legend=True, fontsize=12). To. ax.plot(x='Time',y='Instances',kind='bar', title ="Number of Instances By Time of Day", figsize=(20, 10), legend=True, fontsize=12). And see weather u are getting tick or not – Codewizard_26 Feb 13 '20 at 18:56
  • Because i dont find anything wrong in your code and i have worked on using ticks. So thats why saying you you to do so dude. – Codewizard_26 Feb 13 '20 at 19:00
  • When I try to do just ax.plot... I get the error: NameError: name 'ax' is not defined. – AJHello Feb 13 '20 at 19:06
  • Have a look at this answerhttps://stackoverflow.com/a/11250884/12777391 – Codewizard_26 Feb 13 '20 at 19:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207791/discussion-between-code-wizard-and-ajhello). – Codewizard_26 Feb 13 '20 at 19:11