nilsinelabore
nilsinelabore

Reputation: 5105

How to tell the boundary values of histogram bins in python

The data is a column extracted from another dataframe.

    Result
0   29
1   1
2   1
3   22
4   370
... ...
916 42
917 1
918 2200
919 200
920 770

Here is the graph with code df.hist()

enter image description here

Basically I want to find the boundary values on the x-axis of the first bin so as to separate it from the rest of the bins. How can I do this?

Upvotes: 1

Views: 344

Answers (1)

ZaraA
ZaraA

Reputation: 26

x = df["columnName"]  # your data, a column from a pandas dataframe
no_of_bins = 10       # the default value for the hist function
min_value = min(x)
max_value = max(x)
bin_size = (max_value - min_value)/no_of_bins
print("Bin size", bin_size)
print("Min value", min_value)
print("Max value", max_value)

# Show the bin boundary values
# initiate values
lower_boundary = min_value
upper_boundary = min_value + bin_size

for i in range(no_of_bins):    
    print("[", lower_boundary, "-", upper_boundary, "]")
    lower_boundary = upper_boundary
    upper_boundary = upper_boundary + bin_size

Upvotes: 1

Related Questions