如何使图例跨越两列 [英] how to make a plotly legend span two columns

查看:94
本文介绍了如何使图例跨越两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用绘图图绘制大量数据的比较. 描述每行的字符串往往会变长,因此我需要将它们设置为多行. 但是,在许多情节中,图例可以在屏幕外显示. 我想将图例分为两列. 有办法做到这一点吗?观察help(plotly.graph_objs.Legend),没有ncol或类似matplotlib的参数,而且在可打印的文档中找不到对此的任何引用.

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.

这是一些示例代码:

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)

推荐答案

不幸的是,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/

这篇关于如何使图例跨越两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆