Python Matplotlib:Can';不要创建日志图
我正在迭代数据帧的每一列,并尝试创建如下日志图:Python Matplotlib:Can';不要创建日志图,python,pandas,matplotlib,Python,Pandas,Matplotlib,我正在迭代数据帧的每一列,并尝试创建如下日志图: cols = in_df.columns for col in cols: in_df[col]=in_df[col].dropna() print (in_df[col].values) in_df[col].map(np.log).hist(bins=1000) plt.xlabel(x_label+col) plt.ylabel('Number of customers in train')
cols = in_df.columns
for col in cols:
in_df[col]=in_df[col].dropna()
print (in_df[col].values)
in_df[col].map(np.log).hist(bins=1000)
plt.xlabel(x_label+col)
plt.ylabel('Number of customers in train')
plt.savefig(save_dir+col+'.png')
plt.close()
但我得到了以下错误:
[2 2 2 ..., 2 2 2]
in_df[col].map(np.log).hist(bins=1000)
File "anaconda/envs/kaggle3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 2988, in hist_series
ax.hist(values, bins=bins, **kwds)
File "anaconda/envs/kaggle3/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner
return func(ax, *args, **kwargs)
File "anaconda/envs/kaggle3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5985, in hist
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
File "anaconda/envs/kaggle3/lib/python3.5/site-packages/numpy/lib/function_base.py", line 505, in histogram
'range parameter must be finite.')
ValueError: range parameter must be finite.
请注意,以下工作:
in_df.col_name.map(np.log).hist(bins=1000)
但是,我不能在遍历所有列时使用这种方法。知道我为什么会出错吗?如果我对零的看法是正确的,那么解决问题的最简单方法就是去掉它们。有很多方法可以做到这一点。以下是一个例子:
cols = in_df.columns
for col in cols:
in_df[col]=in_df[col].dropna()
print (in_df[col].values)
# I edited line below
in_df[col].replace(0, np.nan).dropna().map(np.log).hist(bins=1000)
# added |<------------------------>|
plt.xlabel(x_label+col)
plt.ylabel('Number of customers in train')
plt.savefig(save_dir+col+'.png')
plt.close()
cols=in_df.columns
对于col中的col:
in_-df[col]=in_-df[col].dropna()
打印(单位为[col].值)
#我编辑了下面的一行
in_df[col].replace(0,np.nan).dropna().map(np.log).hist(bin=1000)
#增加||
plt.xlabel(x_标签+列)
plt.ylabel(“列车上的客户数量”)
plt.savefig(save_dir+col+'.png')
plt.close()
使用函数既可以保留零,也可以获得有限的范围
你是在拿零的对数吗?嗯,我没想过。里面可能有零
in_df[col].map(np.log1p).hist(bins=1000)