创建一个Python脚本,命名为test1.py,完成以下功能。(1)今有2018年1月1日—15日的猪肉价格和牛肉价格的数据,它们存在于一个Excel表格中,如下表所示。将其读入Python中并用一个数据框变量df保存。日期猪肉价格牛肉价格2018/1/111382018/1/212392018/1/311.541.32018/1/412402018/1/512432018/1/611.2442018/1/713472018/1/812.6432018/1/913.542.32018/1/1013.9422018/1/1113.843.12018/1/1214422018/1/1313.5392018/1/1414.5382018/1/1514.837.5(2)分别绘制1月1日—10日的猪肉价格和牛肉价格走势图。(3)在同一个figure界面中,用一个2×1的子图分别绘制2018年1月前半个月的猪肉价格和牛肉价格走势图。
时间: 2023-04-07 19:02:14 浏览: 198
我可以回答这个问题。以下是Python脚本test1.py的代码,实现了所需功能:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取Excel表格数据
df = pd.read_excel('data.xlsx', index_col=0)
# 绘制1月1日—10日的猪肉价格和牛肉价格走势图
df.loc['2018/1/1':'2018/1/10'].plot()
plt.title('Price Trend from Jan. 1st to Jan. 10th')
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
# 绘制2018年1月前半个月的猪肉价格和牛肉价格走势图
fig, axs = plt.subplots(2, 1, figsize=(8, 6))
df.loc['2018/1/1':'2018/1/15', '猪肉价格'].plot(ax=axs[0])
axs[0].set_title('Pork Price Trend in Jan.')
axs[0].set_xlabel('Date')
axs[0].set_ylabel('Price')
df.loc['2018/1/1':'2018/1/15', '牛肉价格'].plot(ax=axs[1])
axs[1].set_title('Beef Price Trend in Jan.')
axs[1].set_xlabel('Date')
axs[1].set_ylabel('Price')
plt.tight_layout()
plt.show()
```
注意:这里的Excel表格数据需要事先准备好,并保存为data.xlsx文件。