The result is:
This page shows an example of a plot with three waves in one axes using offsets. The PWM wave is the objective.
See also:
Python Matplotlib Tips: Draw several plots in one figure in Python Matplotlib.pyplot
Tips for drawing efficient figures using python matplotlib pyplot. You can brush up them by adding some additional options and settings.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import scipy.signal as signal
t = np.linspace(0, 5/50, 5000)
carwave = signal.sawtooth(2 * np.pi * 500 * t+1/2*np.pi, width=0.5)
sigwave = np.sin(2 * np.pi * 50 * t)
pwmwave = np.ones(t.shape[0])
pwmwave[np.where(sigwave < carwave)] = -1
#t = np.linspace(-4*np.pi,4*np.pi,100)
y1 = carwave
y2 = sigwave
y3 = pwmwave
c1,c2,c3 = "blue","green","red" # colors of each line and label
l1,l2,l3 = "PWM wave","Signal","Career" # labels of each line
o1,o2,o3 = 6,3,0 # offsets of each line
labels1 = [-1,0,1]
labels2 = [-1,0,1]
labels3 = [-1,0,1]
yticks1 = [la+o1 for la in labels1]
yticks2 = [la+o2 for la in labels2]
yticks3 = [la+o3 for la in labels3]
ytls = labels1+labels2+labels3
ytks = yticks1+yticks2+yticks3
plt.figure(figsize=(6,5),facecolor="w")
# plot each line
plt.plot(t*10**3,y3+o3,color=c3,label=l3)
plt.plot(t*10**3,y2+o2,color=c2,label=l2)
plt.plot(t*10**3,y1+o1,color=c1,label=l1)
# plot zero level for each line
plt.plot([t[0]*10**3,t[-1]*10**3],[o1,o1],color=c1,lw=0.5,label="")
plt.plot([t[0]*10**3,t[-1]*10**3],[o2,o2],color=c2,lw=0.5,label="")
plt.plot([t[0]*10**3,t[-1]*10**3],[o3,o3],color=c3,lw=0.5,label="")
plt.ylim(o3-1.5,o1+1.5)
plt.yticks(ytks,ytls)
plt.legend(loc="upper right",fontsize=8)
plt.xlabel("Time [ms]")
labs = plt.yticks()[1]
for i in range(len(labs)):
if i < len(labels1):
labs[i].set_color(c1)
elif i < len(labels1+labels2):
labs[i].set_color(c2)
else:
labs[i].set_color(c3)
plt.savefig("./pwm_in_one_axes_plot.png",dpi=250,bbox_inches="tight",pad_inches=0.02)