<jupyter_start><jupyter_text>Plotting<jupyter_code>from sympy import init_session
init_session(quiet=True)
expr = exp(sin(x**2+pi))
expr.series(x)
expr.series(x, 1, n=7)<jupyter_output><empty_output><jupyter_text>Lets compare the original function with its series expansion visuallySymPy includes plotting functions, which are (by default) based on matplotlib (and also a legacy pyglet-based plotting...)<jupyter_code>%matplotlib inline
plot(expr);
p1 = plot(expr, expr.series(x, 1, 7).removeO(), (x, -4, 7),
    ylim = (-3,3),
    show = False,
    legend = True
)
p1[1].line_color="r"
p1[1].label="series(6)"
p1.show()
p0 = plot(expr, (x, -2.5, 5),
    ylim = (-0,3),
    show = False,
    legend = True
)
for n in range(1, 6):
    p1 = plot(expr.series(x, 1, n=n).removeO(), (x, -2.5, 5), show = False)
    p1[0].line_color = "C%d"%n
    p1[0].label = "n=%d"%(n,)
    p0.append(p1[0])
p0.show()<jupyter_output><empty_output><jupyter_text>For more complicated plots I personally prefer to use matplotlib<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
numexpr = lambdify(x, expr)
xpts = np.linspace(-4, 4, 500)
plt.plot(xpts, numexpr(xpts), label="$%s$"%latex(expr), lw=2)
for n in range(1, 5):
    plt.plot(xpts, lambdify(x, expr.series(x, 1, n=n).removeO())(xpts)*np.ones_like(xpts),
             c="C%d"%n, label="$n=%d$"%n, lw=1)
plt.ylim(0.0, 3)
plt.xlim(-4, 4)
plt.legend(loc="lower left", fontsize=12);<jupyter_output><empty_output><jupyter_text>We have used the lambdify function, which turns a sympy expression into a numerical function (using numpy by default)<jupyter_code>fun = lambdify(x, expr)<jupyter_output><empty_output><jupyter_text>It is a numpy function...<jupyter_code>import inspect
inspect.getsource(fun)<jupyter_output><empty_output><jupyter_text>3D plots are also supported<jupyter_code>from sympy.plotting import plot3d, plot3d_parametric_surface
plot3d(sin(x)*cos(y), (x, -5, 5), (y, -5, 5));
u, v = symbols('u v')
plot3d_parametric_surface(cos(u + v), sin(u - v), u - v, (u, -5, 5), (v, -5, 5));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17286335103814582, 'fraction_numerical': 0.032351521004345726, 'mean_word_length': 5.5362776025236595, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '146219', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 885, 'n_tokens_neox': 847, 'n_words': 190, 'path': 'notebooks/03a Plotting.ipynb', 'repo_name': 'rouckas/sympy-slides', 'size': '2071'}