Python Plotly Legend定位和格式化

时间:2017-01-11 19:28:41

标签: python formatting legend plotly

如何将图例放在图表上方/下方并更改字体大小?我有一个包含12个长传奇名字的图表。将图例放在右侧会影响图表。

此外,我需要将图例名称设为水平。并非垂直线上的所有名称都会太长。

1 个答案:

答案 0 :(得分:2)

您可以在orientation settingslegend属性中通过layout属性设置图例的方向:

layout = plotly.graph_objs.Layout(
    legend=dict(orientation="h")
)

e.g。

import plotly

layout = dict()
trace1 = plotly.graph_objs.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23],
    name="Stackoverflow's personal zoo without any real name but some really long text"
)
trace2 = plotly.graph_objs.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[12, 18, 29],
    name="Another zoo which doesn't have a name but but lots of superfluous characters"
)
trace3 = plotly.graph_objs.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[15, 12, 32],
    name="Yet another zoo with plenty of redundant information"
)

data = [trace1, trace2, trace3]
layout = plotly.graph_objs.Layout(
    barmode='group',
    legend=dict(orientation="h")
)

fig = dict(data=data, layout=layout)

plotly.plotly.sign_in('user', 'token')
plot_url = plotly.plotly.plot(fig)

默认图例 Default legend 水平传奇

Horizontal legend