加入註解

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.5y=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...