python直方图,看起来凌乱且不均匀

问题描述 投票:0回答:1

我有一个数字列表,列表大小为“74004228”,最小值为“1”,最大值为“65852”,我试图了解它们的分布情况,所以我'我使用 plt.hist() 绘制直方图,但它没有给我任何东西。

我得到下面的直方图,看起来很混乱。

matplotlib 代码:

unique_values = sorted(set(dataset_length))
bin_edges = unique_values + [unique_values[-1] + 1]

plt.hist(dataset_length, bins=bin_edges, log=True)  # Align bins to the left
plt.title('Histogram of Data')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()

我已经尝试过了

sns.displot(dataset_length)

sns.distplot 给了我一个空图,如下所示:

有什么解决办法吗?

python matplotlib dataset seaborn histogram
1个回答
0
投票

我记录了数据的日志,看起来更好,但我无法知道每个索引的确切分布。

所以我做了什么:我使用 dict 来计算值,如下

count_dataset_dict = {}
for item in dataset_length:
  if item in count_dataset_dict:
    count_dataset_dict[item] += 1
  else:
    count_dataset_dict[item] = 1

将其转换为 pandas DF,然后过滤阈值 1000

count_df_more_1K = count_df[count_df['count']>1000]

现在我绘制了数据

count_df_more_1K.sort_index().plot(kind='bar', figsize=(18, 8))
plt.title("Distribution of Index")
plt.xlabel("Index")
plt.ylabel("Count")

看起来如下

plot

© www.soinside.com 2019 - 2024. All rights reserved.