簡體   English   中英

使用 matplotlib - Python 創建直方圖

[英]Create Histogram with matplotlib - Python

我正在嘗試使用 matplotlib 創建直方圖(x = 持續時間列;y = 出現次數)但沒有成功。 這是我的代碼:

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()

我不認為代碼有什么問題。 該文件有超過 300 000 行,當我嘗試使用 1000 行的示例運行此代碼時,它工作得很好。 難道問題出在文件的大小上? 您可以從我的github帳戶下載文件。 謝謝你。

一切正常。 問題只是您的持續時間數據分布在從 61 到 1,373,043 的非常寬的范圍內(請參閱df.duration.describe() ),並且在您看來只有一個條形圖有問題:
在此處輸入圖像描述

設置log=True以獲得對數縮放,您會發現一切正常,只是所有條形,但第一個條太小而無法在線性縮放中可見: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM