且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使图例跨越两列

更新时间:2023-11-24 09:14:04

不幸的是,plotly还不支持多列图例.

Unfortunately, plotly does not support multi-column legends yet.

一种解决方法是使用注释.考虑,

One work around would be using annotations. Consider,

import plotly.plotly as py
from plotly.graph_objs import *

import numpy as np
import matplotlib.cm as cm

N = 10
colors=['rgba(%f,%f,%f,%f)' % tuple(c) for c in cm.rainbow((np.linspace(0,1,N)),bytes=True)]

data = []
annotations = []
lab = 'Long<br>Multiline<br>string<br>describing %s'

for i in range(0, N):
    x = np.linspace(0, np.random.uniform(5, 10), 10)
    y = np.linspace(1, np.random.uniform(0, 1), 10)
    label = lab % i

    d = Scatter(
        x=x,y=y,
        mode='lines',
        name=label,text=(label,)*len(x),
        xaxis='x1',yaxis='y1',
        line=Line( color=colors[i]) )
    data.append(d)

    a = Annotation(
        text=label,
        font=Font( color=colors[i] ),
        x=i, y=1,
        xref='page', yref='page'
    )
    annotations.append(a)

layout = Layout(
    showlegend=False,
    annotations=annotations
)

py.plot({'data': data, 'layout': layout}, filename='multi-column-legend')

产生 https://plot.ly/~etpinard/2192/