開始使用免費開始

繪製平均頻率

最後,我們要計算兩個主題標籤每天的平均提及比例,並將其隨時間繪製成圖。我們會先把兩個布林 Series 依天轉為比例,接著再繪圖。

matplotlib.pyplot 已匯入為 plt,而 ds_tweets 也已為你載入。

本練習屬於課程

使用 Python 分析社群媒體資料

檢視課程

練習說明

  • 使用 .resample().mean() 方法,針對 python 欄位產生每天的平均推文數。.resample() 接受一個參數 '1 d',用來產出每日平均。
  • 針對 rstats 欄位做相同的處理。
  • 使用 mean_python 繪製 #python 使用量的折線圖。將 mean_python.index.day 作為 x 軸。
  • mean_rstats 進行相同的繪圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Average of python column by day
mean_python = ____[____].____(____).____()

# Average of rstats column by day
mean_rstats = ____[____].____(____).____()

# Plot mean python by day(green)/mean rstats by day(blue)
plt.plot(____, ____, color = 'green')
plt.plot(____, ____, ____)

# Add labels and show
plt.xlabel('Day'); plt.ylabel('Frequency')
plt.title('Language mentions over time')
plt.legend(('#python', '#rstats'))
plt.show()
編輯並執行程式碼