Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...