เพิ่มสถิติสรุปลงในกราฟ time series
สามารถแสดงกราฟ time series และสถิติสรุปเชิงตัวเลขไว้ในกราฟเดียวกันได้ โดยใช้ pandas API ร่วมกับ matplotlib และเมธอด 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')
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแสดงภาพข้อมูล Time Series ด้วย Python
คำแนะนำการฝึกหัด
ตรวจสอบ meat_mean ใน shell ซึ่งเป็น DataFrame ที่เก็บค่าเฉลี่ยของ time series ทั้งหมดใน meat
- กำหนดค่าทั้งหมดใน
meat_meanให้กับอาร์กิวเมนต์cellText - กำหนดค่าทั้งหมดใน index ของ
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()