開始使用免費開始

清理背景

在探索各州農夫市集販售商品的樣態時,有幾個州特別顯眼。North Dakota 和 New Mexico 在「販售某項商品的市集比例」上經常落在各州的後段班;相對地,Vermont 幾乎總是在前段。你想要呈現各州商品販售的整體趨勢,同時也凸顯你覺得有趣的那些州。

你繪製一張散點圖,x、y 分別是販售的商品與該州販售該商品的市集比例。為了突顯有趣的州,你在各州的點之間畫線。為了讓圖表乾淨、極簡,你把背景簡化為一組基本的導向格線。

本練習屬於課程

用 Python 改善你的資料視覺化

檢視課程

練習說明

  • 將圖表背景設為白色並顯示格線。
  • 在散點圖與折線圖上,分別以正在販售的 'good' 編碼 x 軸、以 'prop selling' 編碼 y 軸。
  • 從圖表中移除「所有」邊框。提醒:預設情況下,sns.despine() 只會移除上方與右側的邊框線(spines)!

動手互動練習

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

# Set background to white with grid
sns.set_style('____')

plt.scatter('____','____', marker = '_', alpha = 0.7, data = goods_by_state)

# Draw lines across goods for highlighted states
highlighted = goods_by_state.query("state in ['New Mexico','North Dakota','Vermont']")
sns.lineplot('____','____', 'state', data = highlighted, legend = False)

# Draw state name at end of lines
last_rows = highlighted.groupby('state', as_index = False).agg('first')
for _,row in last_rows.iterrows():
    plt.annotate(row['state'], (row['good'], row['prop selling']),
                 ha = 'right', xytext = (5,0), textcoords = 'offset pixels')

# Remove all borders
sns.____(____ = ____, ____ = ____)
plt.show()
編輯並執行程式碼