使用 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
以获得对数缩放,您会发现一切正常,只是除了第一个以外的所有条都太小,无法在线性缩放中看到:
我正在尝试使用 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
以获得对数缩放,您会发现一切正常,只是除了第一个以外的所有条都太小,无法在线性缩放中看到: