提升可讀性
你和同事決定,此次最重要的重點是對比最「市場友善」的州 Vermont 與最不友善的 Texas。為了達成這個目的,請將兩個圖並排呈現:一個長條圖顯示各州每個農夫市集所對應的人口數;另一個散佈圖在 x 軸顯示人口數、在 y 軸顯示市集數量。
為了強調重點,請以不同顏色凸顯 Vermont 與 Texas。此外,替 Texas 加上一段大而易讀的註解。
程式中已提供向量 state_colors,其中為 Vermont 與 Texas 指定了獨特顏色,其餘州為灰色;同時也提供了描述 Texas 的註解字串 tx_message。
本練習屬於課程
用 Python 改善你的資料視覺化
練習說明
- 將提供的顏色向量
state_colors透過sns.barplot()的palette參數套用到長條圖(ax1)。 - 以參數
c將顏色向量套用到散佈圖的點。 - 將註解文字大小設為
15,以確保可讀性。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Draw barplot w/ colors mapped to state_colors vector
sns.barplot('people_per_market', 'state', ____ = ____,
data = markets_by_state, ax = ax1)
# Map state colors vector to the scatterplot as well
p = sns.scatterplot('population', 'num_markets', c = ____,
data = markets_by_state, s = 60, ax = ax2)
# Log the x and y scales of our scatter plot so it's easier to read
ax2.set(xscale = "____", yscale = '____')
# Increase annotation text size for legibility
ax2.annotate(tx_message, xy = (26956958,230),
xytext = (26956958, 450),ha = 'right',
size = ____, backgroundcolor = 'white',
arrowprops = {'facecolor':'black', 'width': 3})
sns.set_style('whitegrid')
plt.show()