開始使用免費開始

每月氣溫的圖層疊加

澳洲氣象局希望你協助為其官網製作一些精美的互動式圖表。

他們想同時查看本年度 1 月到 7 月的每日氣溫,並用每月平均氣溫的趨勢線來平滑這些資料點。

這正好是把兩種圖表疊加在一起、達成目標的絕佳示例。

已提供 temp_syd DataFrame,其中包含 2020 年 1 月到 7 月每日(最高)氣溫。你也有 temp_syd_avg DataFrame,包含各月的每日(最高)氣溫平均值。

本練習屬於課程

Python 的 Plotly 資料視覺化入門

檢視課程

練習說明

  • 使用 temp_syd 建立長條圖,並存成 daily_temp_fig
  • 使用 temp_syd_avg 建立折線圖,並存成 monthly_avg_fig
  • 使用 Figure() 建構子將兩張圖合併。

動手互動練習

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

# Create a bar chart for daily temperatures
daily_temp_fig = px.____(temp_syd, 
    x='Date', y='Temp')

# Create a line chart for monthly averages
monthly_avg_fig = px.____(
    temp_syd_avg, x='Date', y='Average', color_discrete_sequence=['red'])

# Combine the figures
combined_fig = Figure(data=[*____.____, *monthly_avg_fig.____])

# Add a title
combined_fig.update_layout(title='Sydney Temperature Analysis')

# Show the combined figure
combined_fig.show()
編輯並執行程式碼