營收長條圖子圖
你先前為紐約證券交易所的一家公司建立了依產業著色(且為堆疊)的營收長條圖,這很有啟發性,因為它顯示了各產業在長條圖上大致分布在哪些區間。
不過,該公司希望不用滑鼠懸停就能看出各產業本身的分布。前一個長條圖在同一張圖裡塞了太多資訊,不利於這種分析,但他們也不想要多張分開的圖。你要如何解這個難題?
你的任務是建立一個依產業分面的公司營收長條圖,作為堆疊的子圖,並使用共享的 x 軸,讓各產業之間能有意義地比較。
已為你載入 revenues DataFrame。
本練習屬於課程
Python 的 Plotly 資料視覺化入門
練習說明
- 建立一個 3 列 1 欄的子圖網格,並共享 x 軸。
- 迴圈走訪選定的產業,為每個產業加入一個長條圖 trace。
- 將每個 trace 放到子圖網格中正確的列位置。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the subplots
fig = make_subplots(rows=____, cols=____, ____=True,
subplot_titles=['Tech Industry Revenue',
'Retail Industry Revenue',
'Professional Services Industry Revenue'])
# Loop through the industries
row_num = 1
for industry in ['Tech', 'Retail', 'Professional Services']:
df = revenues[revenues.Industry == industry]
# Create a histogram
hist = px.____(df, x='Revenue')
# Add the trace to the subplot
fig.____(hist.data[0],
# Position the trace
row=____, col=1)
row_num +=1
# Show the plot
fig.show()