python - 如何为 txt 文件中的给定数据分配适当的 bin 值以绘制直方图
问题描述
我的文本文件中有一组数据。我不确定我应该如何考虑我的 bin 值以便在 python 中使用 matplotlib 绘制直方图。此外,我得到了一些奇怪的量级之王。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
f= np.loadtxt('load.txt', delimiter=',', unpack= True)
# set bins' interval for your data
# You have following intervals:
# 1st col is number of data elements in [0,10000);
# 2nd col is number of data elements in [10000, 20000);
# ...
# last col is number of data elements in [100000, 200000];
#bins = [0,10000,20000,50000,100000,200000]
bins = [0,50,100,150,200]
plt.hist(f, histtype = 'bar', bins = bins)
plt.xlabel('x values')
plt.ylabel('y values')
plt.title('Random plot')
#plt.legend()
plt.show()
data:
1,100
2,10
3,50
4,110
5,120
6,200
解决方案
推荐阅读
- android - Android Webview:页面加载时显示图像
- azure - mavenAuthenticateFeed 在 Azure DevOps MavenTask 中做了什么?
- python - 将带有 K:V 的 3 列 excel 转置为 Pandas 列
- reactjs - 在 ReactJS 中单击按钮时如何添加活动类
- kotlin - 将 lambda 表达式存储在变量中会删除内联优化
- archunit - ArchUnit 可以在方法调用中检查某些字符串模式吗?
- storage - 我应该在哪里为我的应用程序存储少量数据?
- scilab - Scilab gui 上没有文字
- sql - 我无法登录到 postgres,因为我输入了一个空密码
- c++ - 在 C++ 中用字符串和 int 解析(拆分)一个 txt 文件