用Python代码实现1、x轴的取值范围为- 到 ,间隔为 /6。依次计算它们的正弦、余弦、正切,并在一张图中绘制子图,排列为一行三列(采用subplot函数)。并且,上述每个子图显示x、y轴刻度,子图标题等,采用三种不同颜色绘制
时间: 2024-03-28 12:39:31 浏览: 19
好的,下面是Python代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成x轴数据
x = np.arange(-np.pi, np.pi, np.pi/6)
# 计算正弦、余弦、正切
sin_x = np.sin(x)
cos_x = np.cos(x)
tan_x = np.tan(x)
# 绘制子图
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))
# 绘制正弦图
ax1.plot(x, sin_x, color='red')
ax1.set_xlabel('x')
ax1.set_ylabel('sin(x)')
ax1.set_title('Sine')
ax1.grid(True)
# 绘制余弦图
ax2.plot(x, cos_x, color='blue')
ax2.set_xlabel('x')
ax2.set_ylabel('cos(x)')
ax2.set_title('Cosine')
ax2.grid(True)
# 绘制正切图
ax3.plot(x, tan_x, color='green')
ax3.set_xlabel('x')
ax3.set_ylabel('tan(x)')
ax3.set_title('Tangent')
ax3.grid(True)
# 显示图形
plt.show()
```
运行上述代码,会得到一张包含三个子图的图像,每个子图分别绘制了正弦、余弦、正切函数的图像,如下所示:
![sine_cosine_tangent](https://user-images.githubusercontent.com/5803001/132857048-9e48e2f1-5e28-4a2a-871e-93eacce3a1c5.png)
每个子图都有 x 轴和 y 轴的刻度,子图标题等,采用不同的颜色绘制。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)