時系列プロットに要約統計量を追加する
pandas の matplotlib へのAPIと table メソッドを使うと、1つのグラフに時系列プロットと数値の要約を同時に表示できます。
# DataFrame の時系列データをプロット
ax = df.plot()
# df DataFrame の要約統計量を計算
df_summary = df.describe()
# プロットに要約テーブルを追加
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 を確認してください。これは、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()