python - 如何使 plotly 图例跨越两列

标签 python plot plotly

我正在使用绘图图绘制大量数据的比较。 描述每一行的字符串往往会变长,因此我需要将它们设为多行。 然而,随着许多 plotly 的发展,传奇可能会消失在屏幕之外。 我想将图例分成两列。 有没有办法在 plotly 上做到这一点?查看 help(plotly.graph_objs.Legend) 没有 ncol 或与 matplotlib 类似的参数,我在 plotly 文档中找不到对此的任何引用。

下面是一些示例代码:

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 还不支持多列图例。

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

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/

关于python - 如何使 plotly 图例跨越两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189097/

相关文章:

python - 让 Beaker 与 GAE 一起工作

r - 在 R 中绘制跨多个级别的二进制频率

javascript - Django 使用plotly 和表单中的数据绘制图表

r - 将点添加到配对图中?

python - Plotly:如何自定义轴变换?

python mysqldb 表更新不起作用

python - 对正则表达式感到困惑

python 计算 csv pandas 的行数

r - 将ggplot对象转换为在 Shiny 的应用程序中进行绘图

r - plotly 3d surface - 将立方体更改为矩形空间