Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smoothing sympy plots

Tags:

python

plot

sympy

If I have a function such as sin(1/x) I want to plot and show close to 0, how would I smooth out the results in the plot? The default number of sample points is relatively small for what I'm trying to show.

Here's the code:

from sympy import symbols, plot, sin

x = symbols('x')
y = sin(1/x)

plot(y, (x, 0, 0.5))

enter image description here

As x approaches 0, the line becomes more jagged and less "curvy". Is there some way to fix this?

like image 943
MANA624 Avatar asked Feb 14 '16 23:02

MANA624


1 Answers

You can set the number of points used manually:

plot(y, (x, 0.001, 0.5), adaptive=False, nb_of_points=300000)

enter image description here

Note: I expected to get ZeroDivisionError when using the exact question-code (that is, having x go from 0 to something), but i don't get an error (strange). I do get the error though as soon as I use adaptive=False, nb_of_points=300000, so this is why I set xmin to a non zero value (0.001).

like image 51
user Avatar answered Sep 19 '22 12:09

user