各區域價格由高到低
現在你已經知道如何排序 DataFrame,不動產業者希望你建立一個長條圖,將各區域的平均房價由大到小視覺化。
regions 已經透過以 melb 的區域分組並計算平均價格建立完成,並且已為你預先載入:
regions = melb.groupby("region", as_index=False)["price"].mean()
本練習屬於課程
使用 Bokeh 製作互動式資料視覺化
練習說明
- 以降冪順序依
price排序regions。 - 建立圖形,將
x_range設為regions的"region"欄位,並將 x、y 軸分別標示為「Region」與「Sales」。 - 從
regions新增長條圖元,於 y 軸顯示price、x 軸對應各region,並將寬度設為0.9。 - 將 y 軸格式更新為顯示以百萬美元為單位、小數 1 位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Sort df by price in descending order
regions = regions.____("____", ascending=____)
# Create figure
fig = figure(x_range=____, x_axis_label=____, y_axis_label=____)
# Add bar glyphs
fig.vbar(x=____, top=____, width=____)
# Format the y-axis to numeric format
fig.____[____].____ = ____(____="$0.0a")
output_file(filename="sorted_barplot.html")
show(fig)