添加注释

Python 中的 Plotly 数据可视化入门

Alex Scriven

Data Scientist

什么是注释?

  • 在图中添加额外的文本或数据框

$$

  1. 突出某个数据点
  2. 为图表添加备注

数据关联注释

Python 中的 Plotly 数据可视化入门

创建注释

$$

  • 使用 add_annotation()
    • 添加一条注释

$$

  • 使用 update_layout()annotations 参数
    • 接收注释对象列表
Python 中的 Plotly 数据可视化入门

重要的注释参数

  • text:要显示的文本
    • 可包含变量

$$

  • xy:注释的位置

$$

  • showarrow = True/False:是否显示箭头
    • 箭头可自定义

$$

🛑 绝对定位要谨慎——数据变化可能导致重叠

Python 中的 Plotly 数据可视化入门

定位注释

 

  • 默认情况下,xy 指图中的数据值

$$

  • xrefyref 设为 paper 可进行绝对定位
    • 此时 xy 为跨绘图区 0 到 1 的百分比
    • (x=0.5, y=0.5) 位于图中心
Python 中的 Plotly 数据可视化入门

数据关联注释

 

  • 在散点图中高亮公司
my_annotation = dict(
    x=215111, y=449000,

showarrow=True, arrowhead=3,
text="Our company is doing well", font=dict(size=10, color="black") )
fig.update_layout(annotations=[my_annotation]) fig.show()

 

$$

数据关联注释

Python 中的 Plotly 数据可视化入门

浮动注释

$$

float_annotation = dict(
    xref="paper", yref="paper",

x=0.5, y=0.8,
showarrow=False, text="You should <b>BUY</b>", font=dict(size=15, color="black"), bgcolor="rgb(255,0,0)" )

 

浮动注释

Python 中的 Plotly 数据可视化入门

开始练习吧!

Python 中的 Plotly 数据可视化入门

Preparing Video For Download...