Matplotlib
时间: 2023-07-29 16:05:55 浏览: 51
好的,下面是使用Matplotlib绘制带线性刻度标记的正弦函数或余弦函数的六幅小图组成一张图组图的代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y_sin = np.sin(x)
y_cos = np.cos(x)
fig, axs = plt.subplots(2, 3, figsize=(15, 8))
fig.suptitle('Sine and Cosine Functions with Linear Scale Markers')
axs[0, 0].plot(x, y_sin)
axs[0, 0].set_title('Default')
axs[0, 0].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[0, 0].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[0, 1].plot(x, y_sin)
axs[0, 1].set_title('x = 8')
axs[0, 1].set_xlim(8, -8)
axs[0, 1].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[0, 1].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[0, 2].plot(x, y_sin)
axs[0, 2].set_title('x = (30, 8)')
axs[0, 2].set_xlim(30, 8)
axs[0, 2].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[0, 2].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 0].plot(x, y_sin)
axs[1, 0].set_title('x = [16, 24, 32]')
axs[1, 0].set_xticks([16, 24, 32])
axs[1, 0].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 0].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 1].plot(x, y_sin)
axs[1, 1].set_title('x = [0, -1]')
axs[1, 1].set_xlim(0, -1)
axs[1, 1].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 1].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 2].plot(x, y_sin)
axs[1, 2].set_title('x = slice(100, 200, 3)')
axs[1, 2].set_xticks(np.arange(100, 201, 20))
axs[1, 2].xaxis.set_tick_params(which='major', length=5, width=1, direction='in')
axs[1, 2].yaxis.set_tick_params(which='major', length=5, width=1, direction='in')
plt.show()
```
在这个例子中,我们使用`np.linspace`生成了一个包含1000个点的x轴坐标的数组,范围是从-2π到2π。然后,我们使用`np.sin`和`np.cos`函数分别生成了正弦函数和余弦函数的y轴坐标数组。接下来,我们使用`plt.subplots`函数创建了一个2行3列的子图矩阵,并使用`fig.suptitle`函数设置整个图组的标题。然后,我们使用`axs`变量来访问每个子图,并使用`plot`函数绘制正弦或余弦函数的曲线。我们还使用`set_title`函数为每个子图设置标题,并使用`set_xticks`函数设置x轴的刻度值。最后,我们使用`xaxis.set_tick_params`和`yaxis.set_tick_params`函数设置坐标轴的刻度线长度、宽度和方向。最终,使用`plt.show`函数展示整个图组。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)