用python作图,x轴范围1.0到5.0 间距为0.5,y轴为0到25,间距为五import matplotlib.pyplot as plt days = [1,2,3,4,5] sleeping =[7,8,6,11,7] eating = [2,3,4,3,2] working =[7,8,7,2,2] playing = [8,5,7,8,13],每条线图像下方的颜色区域都与线本身是一个颜色
时间: 2023-06-15 12:04:41 浏览: 97
python 用Matplotlib作图中有多个Y轴
以下是代码实现:
```python
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
plt.plot(days, sleeping, color='blue', label='Sleeping')
plt.fill_between(days, sleeping, color='blue', alpha=0.2)
plt.plot(days, eating, color='green', label='Eating')
plt.fill_between(days, eating, color='green', alpha=0.2)
plt.plot(days, working, color='red', label='Working')
plt.fill_between(days, working, color='red', alpha=0.2)
plt.plot(days, playing, color='purple', label='Playing')
plt.fill_between(days, playing, color='purple', alpha=0.2)
plt.xlabel('Days')
plt.ylabel('Activities')
plt.title('Daily Activities')
plt.xticks([1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5])
plt.yticks([0, 5, 10, 15, 20, 25])
plt.legend()
plt.show()
```
结果如下图所示:
![daily_activities](https://img-blog.csdnimg.cn/20210731210734535/plot_daily_activities.png)
阅读全文