Plotly - 圖例



預設情況下,帶有多個跡線的 Plotly 圖會自動顯示圖例。如果它只有一個跡線,則不會自動顯示。要顯示,請將 showlegend 佈局物件的設定為 True。

layout = go.Layoyt(showlegend = True)

圖例的預設標籤是跡線物件名稱。要明確設定圖例標籤,請設定跡線的名稱屬性。

在以下示例中,繪製了兩個帶有名稱屬性的散點圖。

import numpy as np
import math #needed for definition of pi

xpoints = np.arange(0, math.pi*2, 0.05)
y1 = np.sin(xpoints)
y2 = np.cos(xpoints)
trace0 = go.Scatter(
   x = xpoints,
   y = y1,
   name='Sine'
)
trace1 = go.Scatter(
   x = xpoints,
   y = y2,
   name = 'cos'
)
data = [trace0, trace1]
layout = go.Layout(title = "Sine and cos", xaxis = {'title':'angle'}, yaxis = {'title':'value'})
fig = go.Figure(data = data, layout = layout)
iplot(fig)

圖如下所示 −

Legends Trace Object
廣告