Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Handling infinity gradients while working with numpy gradient funcationality

$
0
0

I am using the numpy.gradient() to evaluate the gradients wherein I have points where the x coordinates are constant for a few successive points. It is leading to RuntimeWarning: divide by zero encountered in scalar divide.... while using np.gradient(y, x).

In this context, I am thinking of using the arc tangent of the gradient to avoid infinity or zero-division errors, but not sure how I can proceed.

How do we evaluate gradients (or otherwise their arc tangents) for this kind of data?

A minimal code to reproduce the behaviour.

import numpy as npimport matplotlib.pyplot as pltfrom os import pathCSD = path.dirname(__file__)x = np.array([0.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 5.0])y = np.array([0.0, 2.0, 4.0, 6.0, 9.0, 15.0, 20.0, 30.0])dy_dx = np.gradient(y, x)fig, axs = plt.subplots()axs.plot(x, y, color="b", marker="o", ms=10.0, label="y")axs.plot(x, dy_dx, color="r", marker="*", ms=15.0, label="dy/dx")axs.legend(loc='best')axs.grid()plt.savefig(path.join(CSD, "handling_inf_grad_raw.png"))plt.close()

enter image description here


Viewing all articles
Browse latest Browse all 23131

Trending Articles