简体   繁体   中英

How do you change the x-axis scale/ticks in a bar chart with time values in the x-axis?

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. 在此处输入图片说明

Looking to fix this and any help would be appreciated!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM