開始使用免費開始

在時間序列圖中加入摘要統計

你可以同時在一張圖上視覺化時間序列與數值摘要,方法是結合 pandasmatplotlib 的 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 視覺化時間序列資料

檢視課程

練習說明

先在命令列檢視 meat_mean —— 這個 DataFrame 包含 meat 中所有時間序列的平均值。

  • 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()
編輯並執行程式碼