Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Plotly Legend Positioning and Formatting

How to put the legend above/ below the chart and change the font sizes? I have a chart with 12 long legend names. Putting the legend on the right side will compromise the chart.

Also I need the legend names be horizontal. Not all names on the vertical line it will be too long.

like image 901
thatMeow Avatar asked Jan 11 '17 19:01

thatMeow


1 Answers

You can set the orientation of the legend by via orientation attribute in the legend attribute in the layout settings:

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 Default legend Horizontal legend

Horizontal legend

like image 178
Maximilian Peters Avatar answered Sep 24 '22 05:09

Maximilian Peters