繁体   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