叠加多个图表

Python 中的 Plotly 数据可视化入门

Alex Scriven

Data Scientist

什么是图层叠加?

  • 在同一图中叠加多个图表

$$

GDP 叠加图

  • 无需单独网格位置
  • 使用 add_trace()

$$

$$

  • 无需单独网格位置
  • 使用 add_trace()
Python 中的 Plotly 数据可视化入门

为何要叠加图表?

 

  • 获得更多自定义
    • 例:叠加多条折线

$$

  • 展示互补的图类型

$$

  • 用不同图型引导注意力

$$

  • 更细致地比较数据
Python 中的 Plotly 数据可视化入门

柱状图 + 折线叠加图

 

  • 在同一视图比较精确数值与整体趋势

叠加图示例

Python 中的 Plotly 数据可视化入门

GDP 增长叠加图

from plotly.graph_objects import Figure

layered_fig = Figure()

bar_fig = px.bar(df, x='Date' , y='Quarterly Growth (%)')
line_fig = px.line(df, x='Date' , y='Rolling YTD Growth (%)' , color_discrete_sequence=['red'])
layered_fig = Figure(data= [*bar_fig.data, *line_fig.data]) layered_fig.show()

 

GDP 叠加图

Python 中的 Plotly 数据可视化入门

添加更多图表

 

# 生成相同的 bar_fig、line_fig
line_fig2 = px.line(df, x='Date'
    , y='OECD Average GDP Growth (%)'
    , color_discrete_sequence=['green'])

layered_fig = Figure(data= [*bar_fig.data , *line_fig.data, *line_fig2.data]) layered_fig.show()

 

GDP 叠加图

Python 中的 Plotly 数据可视化入门

后续添加更多图表

 

# 用两个图创建
layered_fig = Figure(data=[*bar_fig.data, *line_fig.data])

# 添加最后一条线 layered_fig.add_trace(line_fig2.data[0])
Python 中的 Plotly 数据可视化入门

开始练习!

Python 中的 Plotly 数据可视化入门

Preparing Video For Download...