'How to plot tan(x).*x in Python?

In MATLAB I can do ezplot("x.*tan(x)") to plot the following:

ezplot tan(x)*x

While in Symbolab it will be like this

Symbolab result

While in Python I use Numpy or Sympy, but still, the graph is so different

from sympy import symbols
from sympy.functions.elementary.trigonometric import tan
from sympy.plotting.plot import plot

x = symbols('x')
eqn = tan(x)*x
plot(eqn, (x, 0, 10), ylim=(-20, 20))

Sympy plot

How can I get an image with Python equivalent to that in MATLAB and Symbolab?



Solution 1:[1]

You can try fplot for a continuous uncertainty. Then limit the plot for x and y axis.

fplot(@(x)x.*tan(x))
xlim([-10,10])
ylim([-5 10])
grid on

Matlab plot The dashed line in plot are the same as the vertical lines in sympy plots.

In python you may use pyplot with markers instead of sympy plot to avoid continuity vertical lines.

from string import whitespace
from sympy import symbols
from sympy import *
from sympy.functions.elementary.trigonometric import tan
from sympy.plotting.plot import plot
import numpy as np
import matplotlib.pyplot as plt

x = symbols('x')
eqn = tan(x)*x

xx = np.linspace(-100, 100, 10000)
yy = lambdify(x, eqn)(xx)
plt.plot(xx,yy,'.')
plt.ylim(-5, 10)
plt.xlim(-20, 20)
plt.grid(True)
plt.show()

Outcome Pyplot image

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1