0

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?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 6
    The image is the same, apart from it running from 0 to 10, rather than -10 to 10 and it plotting asymptotes. I presume your question is thus how to not plot the asymptotes using sympy? – Adriaan Mar 22 '22 at 11:30
  • 1
    Seems like the only way with sympy is to plot the continuous slices separately: https://stackoverflow.com/a/36681255/13138364 – tdy Mar 22 '22 at 14:13
  • 1
    Also see sympy#18455: [Improve singularity handling for plots](https://github.com/sympy/sympy/issues/18455) – tdy Mar 22 '22 at 14:23
  • 1
    And sympy#17738: [Plot of discontinuous piecewise should not connect edges](https://github.com/sympy/sympy/issues/17738) – tdy Mar 22 '22 at 14:32
  • 2
    If you'd use matplotlib with numpy, you could use masking, as in [how to handle an asymptote/discontinuity with Matplotlib](https://stackoverflow.com/questions/2540294/how-to-handle-an-asymptote-discontinuity-with-matplotlib) – JohanC Mar 22 '22 at 16:03

1 Answers1

0

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

im_vutu
  • 412
  • 2
  • 9
  • 1
    Welcome to Stack Overflow. The question is how to do this in Python. Your answer is MATLAB code, but they already know how to do it in MATLAB (and even included their MATLAB code+output in the question). – tdy Mar 24 '22 at 03:16
  • Huh. My mistake. – im_vutu Mar 24 '22 at 06:39