Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...