Plotly 图例
默认情况下,具有多条轨迹的 Plotly 图表会自动显示图例。如果它只有一条迹线,则不会自动显示。要显示,请将 Layout 对象的
showlegend 参数设置为 True。
layout = go.Layoyt(showlegend = true)
图例的默认标签是跟踪对象名称。要设置图例标签,请明确设置跟踪的名称属性。
在下面的例子中,两条散点图与 name 属性一起绘制。
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)
情节如下所示-