John
John

Reputation: 161

getting "ValueError: Axis limits cannot be NaN or Inf" error when plotting PCs

I am doing PC analysis and trying to plot the ranking of PCs using a python package called scanpy (for my data I have to use this package) and this line of code:

>>> sc.pl.pca_variance_ratio(adata, log=True, n_pcs = 50)

but getting the following error:

/home/.local/lib/python3.7/site-packages/scanpy/plotting/_anndata.py:550: RuntimeWarning: divide by zero encountered in log
  scores = np.log(scores)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/.local/lib/python3.7/site-packages/scanpy/plotting/_tools/__init__.py", line 194, in pca_variance_ratio
    log=log,
  File "/home/.local/lib/python3.7/site-packages/scanpy/plotting/_anndata.py", line 616, in ranking
    (1.05 if score_max > 0 else 0.95) * score_max,
  File "/home/.local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 1755, in ylim
    ret = ax.set_ylim(*args, **kwargs)
  File "/home/.local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 4027, in set_ylim
    bottom = self._validate_converted_limits(bottom, self.convert_yunits)
  File "/home/.local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 3614, in _validate_converted_limits
    raise ValueError("Axis limits cannot be NaN or Inf")
ValueError: Axis limits cannot be NaN or Inf

in the command if I remove log=True or set it as False, I won't get error but the plot is not correct. I exported adata into a csv file and looked for NaN and Inf but did not find. do you know how to fix the issue?

Upvotes: 2

Views: 3703

Answers (1)

nobot
nobot

Reputation: 93

It calculates the variance ratios and if you pass log=True, it takes the log of those ratios. That is why you wont see Inf or NaNs in the adata itself.

I suspect some ratios are zero, then when it takes the log, they become -Inf and then when it tries to plot that, it can not.

You can pass log=False and see if some ratios are indeed zero. If they are, lets say ratios after the 30th pc are zero, then pass n_pcs = 30 or less. Then you can pass log=True.

Upvotes: 0

Related Questions