0

sympy piecewise:How can I plot a piecewise function using matplotlib?

Can I easily create a plot using matplotlib?

i want to draw

(ref)

Can I define a regular polyline function in one line?(1)y=x,y=0

i try piecewise

sympy import *
var('x y')
myPw= []
myMx,myMy=2,0
myPw=myPw+list([(0, x < myMx)])
myG=y-1* x
myF=y-2*(x-myMx)+myMy
ans=solve([myG,myF],[x,y])
myPw=myPw+list([(myF,x<=ans[x])])
myMx,myMy=ans[x],ans[y]
myTx,myTy=ans[x],ans[y]
for i in range(2):
    myG=y
    myF=y+2*(x-myTx)-myTy
    ans=solve([myG,myF],[x,y])
    myPw = myPw + list([(solve(myF,y)[0], x <= ans[x])])
    myMx, myMy = ans[x], ans[y]
    myG=(y-myTy)-1*(x-myTx)
    myF=(y-myMy)-2*(x-myMx)
    ans = solve([myG, myF], [x, y])
    myPw = myPw + list([(solve(myF,y)[0], x <= ans[x])])
    myMx,myMy=ans[x],ans[y]
    myTx,myTy=ans[x],ans[y]
print("#",Piecewise(*myPw))
print("#",Piecewise(*myPw).subs({x:30}))
# Piecewise((0, x < 2), (-2*x + y + 4, x <= 4), (12 - 2*x, x <= 6), (2*x - 12, x <= 12), (36 - 2*x, x <= 18), (2*x - 36, x <= 36))
# 24

(20220315) sympy module plot

from sympy import *
var('x y')
def myCal():
    myPw = []
    myMx, myMy = 2, 0
    myPw = myPw + list([(0, x <= myMx)])
    myG = y - 1 * x
    myF = y - 2 * (x - myMx) + myMy
    ans = solve([myG, myF], [x, y])
    myPw = myPw + list([(solve(myF, y)[0], x <= ans[x])])
    myMx, myMy = ans[x], ans[y]
    myTx, myTy = ans[x], ans[y]
    for i in range(2):
        myG = y
        myF = y + 2 * (x - myTx) - myTy
        ans = solve([myG, myF], [x, y])
        myPw = myPw + list([(solve(myF, y)[0], x <= ans[x])])
        myMx, myMy = ans[x], ans[y]
        myG = (y - myTy) - 1 * (x - myTx)
        myF = (y - myMy) - 2 * (x - myMx)
        ans = solve([myG, myF], [x, y])
        myPw = myPw + list([(solve(myF, y)[0], x <= ans[x])])
        myMx, myMy = ans[x], ans[y]
        myTx, myTy = ans[x], ans[y]
    print("#", Piecewise(*myPw))
    return myPw
def main():
    plot(Piecewise(*myCal()))
if __name__ == '__main__':
    main()

(20220317) i remove my

i use SymPy Plotting Module

enter image description here

from sympy import *
var('x y')
def Cal():
    Pw = []
    Mx, My = 2, 0
    Pw = Pw + list([(0, x <= Mx)])
    G = y - 1 * x
    F = y - 2 * (x - Mx) + My
    ans = solve([G, F], [x, y])
    Pw = Pw + list([(solve(F, y)[0], x <= ans[x])])
    Mx, My = ans[x], ans[y]
    Tx, Ty = ans[x], ans[y]
    for i in range(2):
        G = y
        F = y + 2 * (x - Tx) - Ty
        ans = solve([G, F], [x, y])
        Pw = Pw + list([(solve(F, y)[0], x <= ans[x])])
        Mx, My = ans[x], ans[y]
        G = (y - Ty) - 1 * (x - Tx)
        F = (y - My) - 2 * (x - Mx)
        ans = solve([G, F], [x, y])
        Pw = Pw + list([(solve(F, y)[0], x <= ans[x])])
        Mx, My = ans[x], ans[y]
        Tx, Ty = ans[x], ans[y]
    return Pw,My
def main():
    Pw  =Cal()[0]
    Hmax=Cal()[1]
    plot(Piecewise(*Pw),(x,0,Hmax))
if __name__ == '__main__':
    main()

save the figure: dialog command

mrrclb46z
  • 89
  • 6

0 Answers0