第一关
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='darkgrid')
plt.tight_layout()
df = pd.read_csv("src/step1/data.csv") # 读入数据
def student():
########## Begin #########
fig, ax = plt.subplots(1, 5, figsize=(12, 3))
ax[0].set_title("happiness=1")
df[df['happiness'] == 1]['county'].hist(bins=10, ax=ax[0])
ax[1].set_title("happiness=2")
df[df['happiness'] == 2]['county'].hist(bins=10, ax=ax[1])
ax[2].set_title("happiness=3")
df[df['happiness'] == 3]['county'].hist(bins=10, ax=ax[2])
ax[3].set_title("happiness=4")
df[df['happiness'] == 4]['county'].hist(bins=10, ax=ax[3])
ax[4].set_title("happiness=5")
df[df['happiness'] == 5]['county'].hist(bins=10, ax=ax[4])
# 确保所有子图布局紧凑
#plt.tight_layout()
# 保存图形
fig.savefig('photo/step1/stu_ph/stu.jpg')
########## End #########
# 调用函数
student()
第四关
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
def student():
data = pd.read_csv("src/step4/data.csv", index_col='Month')
data.index = pd.date_range(start=data.index[0], periods=len(data.index), freq='M')
########## Begin #########
decomposition = seasonal_decompose(data) # data:需要解析的数据
trend = decomposition.trend # 趋势
seasonal = decomposition.seasonal # 周期
residual = decomposition.resid # 残差
plt.subplot(4, 1, 1)
plt.plot(data, linestyle='-', color='red', alpha=0.8)
plt.title('Source data')
plt.subplot(4, 1, 2)
plt.plot(trend, linestyle='-', color='red', alpha=0.8)
plt.title('Trend info')
plt.subplot(4, 1, 3)
plt.plot(seasonal, linestyle='-', color='red', alpha=0.8)
plt.title('Seasonal info')
plt.subplot(4, 1, 4)
plt.plot(residual, linestyle='-', color='red', alpha=0.8)
plt.title('Residual info')
########## End #########
plt.tight_layout()
plt.savefig("photo/step4/stu_ph/stu.jpg")