如何在同一张图中,在域 上绘制以下三个函数(即sin和cos加法) ?t
sin
cos
t
import numpy as np import matplotlib.pyplot as plt t = np.linspace(0, 2*np.pi, 400) a = np.sin(t) b = np.cos(t) c = a + b
您可以使用 Matplotlib 在同一张图中绘制多个函数,方法是调用plt.plot()要绘制的每个函数的函数。以下是如何在同一张图中绘制sin、cos及其总和:
plt.plot()
import numpy as np import matplotlib.pyplot as plt # Generate the domain t from 0 to 2π t = np.linspace(0, 2 * np.pi, 400) # Define the functions a = np.sin(t) b = np.cos(t) c = a + b # Plot each function on the same figure plt.plot(t, a, label='sin(t)') plt.plot(t, b, label='cos(t)') plt.plot(t, c, label='sin(t) + cos(t)') # Add a legend to differentiate the functions plt.legend() # Add labels and title for clarity plt.xlabel('t') plt.ylabel('Value') plt.title('Plot of sin(t), cos(t), and sin(t) + cos(t)') # Show the plot plt.show()
plt.plot(t, a, label='sin(t)')
plt.plot(t, b, label='cos(t)')
plt.plot(t, c, label='sin(t) + cos(t)')
plt.legend()
plt.xlabel('t')
plt.ylabel('Value')
plt.title('Plot of sin(t), cos(t), and sin(t) + cos(t)')
这将在同一张图表上显示所有三个函数,用不同的颜色和图例表示哪个函数是哪个。