用python作图,x轴范围1.0到5.0 间距为0.5,y轴为0到25,间距为五import matplotlib.pyplot as plt days = [1,2,3,4,5] 睡眠 =[7,8,6,11,7] 进食 = [2,3,4,3,2] 工作 =[7,8,7,2,2] 播放 = [8,5,7,8,13],每条线图像下方的颜色区域都与线本身是一个颜色,四条线一个落在一个的上面 分为四个色块
时间: 2023-06-15 09:04:40 浏览: 85
Python matplotlib学习笔记之坐标轴范围
可以使用如下代码绘制所需的图形:
```python
import matplotlib.pyplot as plt
# 设置数据
days = [1, 2, 3, 4, 5]
sleep = [7, 8, 6, 11, 7]
eat = [2, 3, 4, 3, 2]
work = [7, 8, 7, 2, 2]
play = [8, 5, 7, 8, 13]
# 绘制图形
plt.figure(figsize=(8, 6)) # 设置图像大小
plt.plot(days, sleep, label='Sleep', color='blue') # 绘制睡眠线
plt.plot(days, eat, label='Eat', color='green') # 绘制进食线
plt.plot(days, work, label='Work', color='red') # 绘制工作线
plt.plot(days, play, label='Play', color='purple') # 绘制娱乐线
# 设置坐标轴范围和间距
plt.xticks([1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5])
plt.yticks([0, 5, 10, 15, 20, 25])
plt.xlim(1, 5)
plt.ylim(0, 25)
# 填充颜色区域
plt.fill_between(days, sleep, color='blue', alpha=0.2)
plt.fill_between(days, eat, sleep, color='green', alpha=0.2)
plt.fill_between(days, work, eat, color='red', alpha=0.2)
plt.fill_between(days, play, work, color='purple', alpha=0.2)
# 添加图例和标签
plt.legend(loc='upper left')
plt.xlabel('Days')
plt.ylabel('Hours')
plt.title('Daily Activities')
# 显示图像
plt.show()
```
这段代码会生成如下的图像:
![image.png](attachment:image.png)
阅读全文