I have tried using masks and making lists like x = [0,0.5,0.51,1,1.01], y = [1,1,-1,-1,1] works ofcourse, but is quite tedious and not as nice as i want to make a square wave from x = 0 to 5.
Advertisement
Answer
You could write a little function like this to populate x and y
JavaScript
x
33
33
1
import matplotlib.pyplot as plt
2
import math
3
4
def SquareWave(high, low, start, end, width):
5
print("Square Wave")
6
##Calculate how many cycles
7
ncycles = int(math.ceil((end - start)/width)) #Round up
8
print(ncycles)
9
x=[]
10
y=[]
11
x.append(start)
12
y.append(high)
13
xstep = width / 2
14
for n in range(ncycles):
15
start += xstep #Increment by the step width
16
x.append(start)
17
y.append(high)
18
x.append(start)
19
y.append(low)
20
start += xstep #Increment by the step width
21
x.append(start)
22
y.append(low)
23
x.append(start)
24
y.append(high)
25
return(x,y)
26
27
28
29
x,y = SquareWave(1,-1, 0, 5, 1)
30
31
plt.plot(x,y)
32
33