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
广告
© . All rights reserved.