AbtPst
AbtPst

Reputation: 8008

Matplotlib : Can't create log plots

I am iterating over each column of a dataframe and trying to create log plots as

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()

but I get the following error:

[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.

Note that the following works:

in_df.col_name.map(np.log).hist(bins=1000)

However, I can't use this approach while iterating over all of the columns. Any idea why I am getting the error?

Upvotes: 2

Views: 787

Answers (2)

taras
taras

Reputation: 6914

It is possible to both retain zeros and get finite range with log(x + 1) function

in_df[col].map(np.log1p).hist(bins=1000)

Upvotes: 1

piRSquared
piRSquared

Reputation: 294258

If I'm right about the zeros, easiest way to fix your problem is to drop them. There are a bunch of ways to do it. Below is one:

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()

Upvotes: 1

Related Questions