開始使用免費開始

各州販售商品的熱門度

農夫市集資料集包含 28 種不同商品的欄位,以及該市集是否販售該商品。你想看看,在各州的市集中,找到特定商品的「可能性」是否有有趣的趨勢。為了回答這個問題,你將資料彙整成三個欄位:

  • state-州名
  • good-關注的商品
  • prop_selling-該州販售「該」商品的市集所占比例

為了快速判斷是否出現某些模式,你挑選一小部分你感興趣的商品,並決定製作一張簡單的文字散佈圖:x 軸是商品,y 軸是該州市集中販售該商品的比例。

本練習屬於課程

用 Python 改善你的資料視覺化

檢視課程

練習說明

  • goods_by_state 篩選為 to_plot 中列出的目標商品。

  • 透過將點的大小設為空值來隱藏散佈圖的點。

    • 注意:在 sns.scatterplot() 中,size 用來把某欄位的數值對映到一組大小尺度;而 s 則是為所有點設定相同的固定大小。
  • 將文字置中對齊,讓它剛好落在對應商品的 x 軸位置上。

動手互動練習

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

# Subset goods to interesting ones
to_plot = ['Cheese','Maple','Fruits','Grains','Seafood','Plants','Vegetables']
goods_by_state_small = goods_by_state.____("good in "+str(to_plot))

g = sns.scatterplot('good','prop_selling', data = goods_by_state_small,
                    # Hide scatter points by shrinking to nothing
                    ____ = ____)

for _,row in goods_by_state_small.iterrows():
  g.annotate(row['state'], (row['good'], row['prop_selling']), 
             # Center annotation on axis
             ha = '____', 
             size = 10)

plt.show()
編輯並執行程式碼