Histogram per hour - matplotlib
up vote
0
down vote
favorite
I'm analyzing public data on transport accidents in the UK.
My dataframe looks like this :
Index Time
0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05
I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')
This is the output:
In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?
python matplotlib histogram pandas-groupby
|
show 2 more comments
up vote
0
down vote
favorite
I'm analyzing public data on transport accidents in the UK.
My dataframe looks like this :
Index Time
0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05
I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')
This is the output:
In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?
python matplotlib histogram pandas-groupby
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
1
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm analyzing public data on transport accidents in the UK.
My dataframe looks like this :
Index Time
0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05
I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')
This is the output:
In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?
python matplotlib histogram pandas-groupby
I'm analyzing public data on transport accidents in the UK.
My dataframe looks like this :
Index Time
0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05
I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')
This is the output:
In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?
python matplotlib histogram pandas-groupby
python matplotlib histogram pandas-groupby
edited Nov 11 at 3:15
Matt
750920
750920
asked Nov 10 at 23:55
Fra
32
32
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
1
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38
|
show 2 more comments
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
1
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
1
1
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
What you are missing is setting the format of the matplotlib x-axis format:
df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
What you are missing is setting the format of the matplotlib x-axis format:
df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()
add a comment |
up vote
1
down vote
accepted
What you are missing is setting the format of the matplotlib x-axis format:
df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
What you are missing is setting the format of the matplotlib x-axis format:
df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()
What you are missing is setting the format of the matplotlib x-axis format:
df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()
edited Nov 11 at 12:02
answered Nov 11 at 4:22
mostafazh
1,523718
1,523718
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244590%2fhistogram-per-hour-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47
1
@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41
No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58
@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39
@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38