Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a plotly legend span two columns

I am plotting a comparison of lots of data using a plotly graph. The strings describing each line tend to get long, so I need to make them multiline. However, with many plots, the legend can run off the screen. I would like to split the legend into two columns. Is there a way to do this in plotly? Looking at help(plotly.graph_objs.Legend) there is no ncol or similar argument as matplotlib has, and I can't find any reference to this in the plotly documentation.

Here is some example code:

import numpy as np
import matplotlib.cm as cm
py.sign_in("XXX", "XXXX")
N = 10
colors=['rgba(%f,%f,%f,%f)' % tuple(c) for c in cm.rainbow((np.linspace(0,1,N)),bytes=True)]
data = []
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)   
py.iplot(data)
like image 981
jmerkow Avatar asked Mar 21 '15 23:03

jmerkow


1 Answers

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')

which yields https://plot.ly/~etpinard/2192/

like image 141
etpinard Avatar answered Oct 20 '22 19:10

etpinard