Plot Piecewise Function in Python

后端 未结 5 469
[愿得一人]
[愿得一人] 2021-01-02 10:56

I would like to plot the following piecewise function in Python using Matplotlib, from 0 to 5.

f(x) = 1, x != 2; f(x) = 0, x = 2

In Python...

5条回答
  •  情歌与酒
    2021-01-02 11:15

    the append works but requires a little extra processing. np's piecewise works fine. could just do this for any function:

    `

    import math
    import matplotlib as plt
    
    xs=[]
    xs=[x/10 for x in range(-50,50)]   #counts in tenths from -5 to 5
    
    plt.plot(xs,[f(x) for x in xs])
    

    `

提交回复
热议问题