总结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()

enter image description here

输出应类似于以下内容

enter image description here

解决方法

您的意思是:

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

输出:

enter image description here