我有一组数据,一维的,杂乱无章,想要快速知道数据的分布情况。最开始的想法是,弄一个数组,遍历数据落在每个区间的个数,最后画出分布,或者输出相关信息,为此我写了一个循环:
# Check the distribution
j = np.zeros(6)
jj = []
for jjj in range(len(j)):
jj.append(np.round(pow(0.1, jjj+1), 8))
for i in range(len(f_nan_0)):
for ii in range(len(j)):
if f_nan_0[i] < jj[ii]:
j[ii] += 1
print('Energy cut is:')
print(' '.join(map(str, jj)))
print('Labels below energy cut:')
print(' '.join(map(str, j)))
print('Percentage below energy cut:')
print(' '.join(map(str, np.round(j/len(f_nan_0), 4))))
输出如下:
数据在排序后,绘图,大概长这样:
以上可以满足要求,但是并不详细。我们只是大概知道数据在某些区间的个数,再具体的信息,比如,落在0.1~0.3的个数无从得知。
后来我了解了seaborn,算是matplotlib的升级版,导入seabron包以后,3句话就实现了数据发布可视化:
fig = plt.figure()
sns.displot(force)
plt.show()
结果如下:
这种方式相对于以上两种,可以反馈出更详细的信息,而且形式更简洁,就像正常的matplotlib画图一样,把plt.plot换成sns.displot即可