为时间序列图添加汇总统计
通过结合使用 pandas 提供给 matplotlib 的 API 和 table 方法,您可以在同一张图上同时展示时间序列曲线和数值汇总信息:
# Plot the time series data in the DataFrame
ax = df.plot()
# Compute summary statistics of the df DataFrame
df_summary = df.describe()
# Add summary table information to the plot
ax.table(cellText=df_summary.values,
colWidths=[0.3]*len(df.columns),
rowLabels=df_summary.index,
colLabels=df_summary.columns,
loc='top')
本练习是课程的一部分
用 Python 可视化时间序列数据
练习说明
请在 Shell 中查看 meat_mean —— 一个包含 meat 中所有时间序列均值的 DataFrame。
- 将
meat_mean的所有数值传给cellText参数。 - 将
meat_mean的索引传给rowLabels参数。 - 将
meat_mean的列名传给colLabels参数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Plot the meat data
ax = meat.plot(fontsize=6, linewidth=1)
# Add x-axis labels
ax.set_xlabel('Date', fontsize=6)
# Add summary table information to the plot
ax.table(cellText=meat_mean.____,
colWidths = [0.15]*len(meat_mean.columns),
rowLabels=meat_mean.____,
colLabels=meat_mean.____,
loc='top')
# Specify the fontsize and location of your legend
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 0.95), ncol=3, fontsize=6)
# Show plot
plt.show()