总结 python 中多个函数的输出

Summing up the output of multiple functions in python

我目前有三个正弦函数 (y1, y2, y3),并且想在一个新函数 (ytotal) 中对函数的输出求和,但仅限于正弦函数的输出大于 0 的情况。

import numpy as np
import matplotlib.pyplot as plt
#%%

phi = np.linspace(-2*np.pi, 2*np.pi, 100)

y1 = 0.2*np.sin(phi)
y2 = 0.2*np.sin(phi-(120*(np.pi/180)))
y3 = 0.2*np.sin(phi-(240*(np.pi/180)))

#if y1 or y2 or y3 > 0:
#    ytotal = y1+y2+y3


    


plt.plot(phi,y1, label = "Piston 1")
plt.plot(phi,y2, label = "Piston 2")
plt.plot(phi,y3, label = "Piston 3")
#plt.plot(phi,ytotal, label = "Total output")
positions = (0,np.pi/3,2*np.pi/3,np.pi,4*np.pi/3,5*np.pi/3,2*np.pi)
labels = ("0","60","120","180","240","300","360")
plt.xticks(positions, labels)
plt.xlabel('Angular displacement')
plt.ylabel('Stroke')
plt.legend()
plt.show()

输出应该如下所示:

你是说:

plt.plot(phi, y1.clip(0)+y2.clip(0)+y3.clip(0), label='Total')

输出: