Python直方图ValueError:范围参数必须是有限的 [英] Python Histogram ValueError: range parameter must be finite

查看:49
本文介绍了Python直方图ValueError:范围参数必须是有限的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用直方图绘制熊猫数据框时,

when plotting Pandas dataframe using a histogram,

样本数据帧数据

     distance
0    5.680195
2    0.000000
3    7.974658
4    2.461387
5    9.703089

我用来绘图的代码

import matplotlib.pyplot as plt

plt.hist(df['distance'].values)
plt.show()

我有这个错误

"ValueError: range parameter must be finite."  

我的尝试

df['Round_Distance'] = df['distance'].round(1)

0    5.7
2    0.0
3    8.0
4    2.5
5    9.7

再次绘制,新错误

plt.hist(df['Round_Distance'].values)
plt.show()

ValueError: max must be larger than min in range parameter.

奇怪的是,我使用的解决方法如下,我不必ROUND

weird thing is, the work around i use is below, i don't have to ROUND

df['distance'].hist(bins=[0,25,50,75,100,125,150,175], color='g')

推荐答案

听起来您的实际数据中有一些 NaNinf.您只能选择像这样有限的值:

Sounds like you have some NaNs or inf in your actual data. You can select only those values that are finite like this:

import numpy as np

df[np.isfinite(df['distance'])]

所以您的情节可以像这样获得:

So your plot can be obtained like:

plt.hist(df[np.isfinite(df['distance'])].values)

这篇关于Python直方图ValueError:范围参数必须是有限的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆