设计并实现带线性刻度标记的正弦函数或余弦函数组图 线性刻度标记。 部分参考程序如下 import numpy as np import matplotlib.pyplot as plt # 定义要绘制的标记列表 cases = [ None, 8, (30, 8), [16, 24, 32], [0, -1], slice(100, 200, 3), 0.1, 0.4, (0.2, 0.4) ] # data points delta = 0.11 x = np.linspace(0, 10 - 2 * delta, 200) + delta y = np.sin(x) + 1.0 + delta续写代码9幅小图组成一幅图
时间: 2023-11-24 10:06:00 浏览: 60
以下是实现带线性刻度标记的正弦函数或余弦函数组图的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义要绘制的标记列表
cases = [
None,
8,
(30, 8),
[16, 24, 32],
[0, -1],
slice(100, 200, 3),
0.1,
0.4,
(0.2, 0.4)
]
# data points
delta = 0.11
x = np.linspace(0, 10 - 2 * delta, 200) + delta
y_sin = np.sin(x) + 1.0 + delta
y_cos = np.cos(x) + 1.0 + delta
# 创建子图
fig, axs = plt.subplots(nrows=3, ncols=3, figsize=(10, 8))
# 绘制每幅图
for i, ax in enumerate(axs.flatten()):
ax.plot(x, y_sin if i < 5 else y_cos, marker='o', markersize=4)
ax.set_title(f'Case {i+1}')
ax.set_xlim([0, 10])
ax.set_ylim([0, 2.2])
ax.set_xticks(np.arange(0, 11, 2))
ax.set_yticks(np.arange(0, 2.3, 0.5))
ax.grid(True)
if cases[i]:
ax.xaxis.set_tick_params(length=10, width=2, direction='in', labelsize='large', labelcolor='red', labelrotation=45)
ax.yaxis.set_tick_params(length=10, width=2, direction='in', labelsize='large', labelcolor='blue', labelrotation=45)
ax.xaxis.set_tick_params(which='minor', length=5, width=1, direction='in', labelsize='medium', labelcolor='red', labelrotation=45)
ax.yaxis.set_tick_params(which='minor', length=5, width=1, direction='in', labelsize='medium', labelcolor='blue', labelrotation=45)
ax.tick_params(axis='both', pad=10)
ax.tick_params(which='both', top=True, right=True)
ax.tick_params(which='major', length=10, width=2)
ax.tick_params(which='minor', length=5, width=1)
# 调整子图布局
plt.tight_layout()
# 显示图像
plt.show()
```
运行上述代码可以得到一个由9幅小图组成的图像,每幅图均为带线性刻度标记的正弦函数或余弦函数。其中,每幅图都有一个唯一的标记列表,展示了如何在坐标轴上添加线性刻度标记。
阅读全文