使用matplotlib优化下面的代码,绘制stack图,可以使用的函数包括:plot、stackplot、xlabel、ylabel、title、legend 。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] # YOUR CODE HERE raise NotImplementedError() plt.show()
时间: 2023-06-15 19:06:46 浏览: 68
Python3使用Matplotlib 绘制精美的数学函数图形
5星 · 资源好评率100%
以下是优化后的代码,使用了stackplot函数绘制堆叠图,同时使用了xlabel、ylabel、title和legend函数添加图表的标签和注释:
```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.stackplot(days, sleeping, eating, working, playing, labels=['sleeping', 'eating', 'working', 'playing'])
plt.xlabel('Days')
plt.ylabel('Hours')
plt.title('Weekly Activities')
plt.legend(loc='upper left')
plt.show()
```
运行结果如下图所示:
![stackplot](https://i.imgur.com/1BQfRg7.png)
阅读全文