python代码,使用matplotlib简易画出y=2cosx+3sinx图像,带上坐标轴的箭头以及对y的红色表示?(8分)
时间: 2023-05-29 16:04:11 浏览: 162
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = 2*np.cos(x) + 3*np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, color='blue')
ax.arrow(0, 0, 2, 0, length_includes_head=True, head_width=0.1, head_length=0.2, color='black')
ax.arrow(0, 0, 0, 4, length_includes_head=True, head_width=0.1, head_length=0.2, color='black')
ax.text(1.8, 0.2, 'x', fontsize=12)
ax.text(0.2, 3.8, 'y', fontsize=12)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.7))
ax.set_xlim(-0.2, 2*np.pi+0.2)
ax.set_ylim(-5, 5)
ax.fill_between(x, y, where=(y>=0), interpolate=True, color='red')
ax.fill_between(x, y, where=(y<=0), interpolate=True, color='blue')
plt.show()
```
运行结果:
![image.png](attachment:image.png)
阅读全文