1

I have a Pandas DataFrame in the form of:

hour   activity
1      23
2      334
3      345
...
23     24
24     12

I just want to plot a histogram with 1-24 as bins and corresponding activity as value (height). Is there any simple way doing this in Pandas?

2 Answers 2

2

I think you could use df.plot for this:

df.plot('hour', 'activity', kind='bar')

This uses 'hour' values as the x-axis and 'activity' as the y-axis, specifying that a bar chart should be drawn.

Example:

>>> hour = range(1, 25)
>>> activity = np.random.randint(1, 10, 24)
>>> df = pd.DataFrame({'hour': hour, 'activity': activity})
>>> df.plot('hour', 'activity', kind='bar');

Which produces:

enter image description here

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

Comments

1

You can use:

df.plot('hour','activity',kind='bar',rot=0)

where df is your dataframe, rot is added to rotate the labels

1 Comment

that doesn't work, when hour is index? because when I used df.hist(), it takes my activity as bins and hour as y value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.