Home > Software design >  What is the difference between (matplotlib) and (SymPy Plotting Module)?
What is the difference between (matplotlib) and (SymPy Plotting Module)?

Time:03-18

enter image description here

sympy plot of piecewise functions

Although this works, it is not sympy's standard way. Usually, functions are written as expressions. Note how f3 is used as an expression that contains x, instead of the less flexible f3(x) of the previous example.

from sympy import plot, Symbol, Piecewise, And

x = Symbol('x', real=True)
n = Symbol('n', real=True)

fn = Piecewise((x - n, (n <= x) & (x <= n   1)),
               (2 - x   n, (n   1 <= x) & (x <= n   2)),
               (0, True))

f3 = fn.subs(n, 3)  # Piecewise((x - 3, (x >= 3) & (x <= 4)), (5 - x, (x >= 4) & (x <= 5)), (0, True))
f8 = fn.subs(n, 8)  # Piecewise((x - 8, (x >= 8) & (x <= 9)), (10 - x, (x >= 9) & (x <= 10)), (0, True))

plot(f3, f8, (x, -1, 11))
  • Related