Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10000)
plt.plot(x, np.tan(x))
plt.show()
Expected result:
The result I get:
tan(array[, out]) = ufunc 'tan') : This mathematical function helps user to calculate trigonometric tangent for all x(being the array elements). Parameters : array : [array_like]elements are in radians.
For plotting graphs in Python, we will use the Matplotlib library. Matplotlib is used along with NumPy data to plot any type of graph. From matplotlib we use the specific function i.e. pyplot(), which is used to plot two-dimensional data.
There are two issues, I think. The first is about np.linspace
, the second about plotting.
np.linspace
defaults to returning 50 elements within the given range. So you're plotting 50 points over (0, 10000)
, meaning the elements are very widely spaced. Also, that range doesn't make a whole lot of sense for the tangent function. I'd use something much smaller, probably +/- 2 * pi.
The second issue is the y-axis. The tangent function diverges to infinity quite quickly at multiples of pi/2
, which means you're missing a lot of the interesting behavior by plotting the full y-range. The code below should solve these issues.
x = np.linspace(-2 * np.pi, 2 * np.pi, 1000)
plt.plot(x, np.tan(x))
plt.ylim(-5, 5)
You should see something like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With