asPlankBridge
asPlankBridge

Reputation: 1082

change bin size of histogram data

I have data from an SQL statement like

'select action_count, count(*) "num_users" from customers group by action_count;'

and load this into a pandas data frame. I can run a simple plot command on the data, or barplot it to make it look like a histogram. But how can I easily change the bin size of this data set? And how do I then plot the bins, i.e. the x-axis correctly?

Thanks!

Upvotes: 3

Views: 7692

Answers (1)

Ed Smith
Ed Smith

Reputation: 13216

You can specify the location of the edges of bins using a list in pandas hist. For example, using a custom sequence with bin from -2.0 to -0.5 and then -0.5 to 0.0 followed by a few increments of 0.1,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df =pd.DataFrame({'col1':np.random.randn(10000)})
df.hist(bins=[-2.,-0.5,0.,0.1,0.2,0.3])   
plt.show()

which plots,

enter image description here

Upvotes: 3

Related Questions