Michal
Michal

Reputation: 259

Create Histogram with matplotlib - Python

I'm trying to create histogram (x = duration column; y = count of occurrences) with matplotlib but without success. Here is my code:

import pandas as pd 
import matplotlib.pyplot as plt

df = pd.read_excel ("J:/edinburgh_bikes.xlsx")
x = df['duration'].to_numpy()

fig, ax = plt.subplots()

# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)

plt.xlabel('duration')
plt.ylabel('count')
plt.title('Histogram of bike ride duration')
plt.grid(True)
plt.show()

I dont think there is anything wrong with the code. The file has over 300 000 rows and when I tried run this code with sample of 1000 rows it worked just fine. Could it be that the problem is the size of the file? You can download the file from my github account. Thank you.

Upvotes: 0

Views: 92

Answers (1)

Stef
Stef

Reputation: 30639

Everything works correctly. The issue is just that your duration data are spread over a very wide range from 61 to 1,373,043 (see df.duration.describe()) and it seems to you that there's something wrong as you see just one bar:
enter image description here

Set log=True to get a log scaling and you'll discover that everything is OK, just all bars but the first one are too small to be visible in a linear scaling: enter image description here

Upvotes: 2

Related Questions