What is the coordinate axes for legend

What is the coordinate axes for the legend?

Where is the origin and what is the range of x and y (for the legend)

I have a graph with many categories. I am having problems managing the legend.

Here is how I create the chart:
traces1 = []

for i in range(0,100):
traces1.append(go.Bar(
x = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9],
y = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9],
name = β€˜column’ + str(i)
))

layout0 = go.Layout(
width=1000, height=500,
legend=dict(orientation=β€œh”, x = 0,y = -2, yanchor=β€œbottom”)
)

fig1 = go.Figure(data=traces1, layout=layout0)

offline.iplot(fig1)

Hi @nilanjenator,

Here’s how to display the documentation for all of the legend properties from within ipython/jupyter

go.layout.Legend?
Init signature:
go.layout.Legend(
    ['arg=None', 'bgcolor=None', 'bordercolor=None', 'borderwidth=None', 'font=None', 'orientation=None', 'tracegroupgap=None', 'traceorder=None', 'uirevision=None', 'valign=None', 'x=None', 'xanchor=None', 'y=None', 'yanchor=None', '**kwargs'],
)
Docstring:      Base class for all types in the layout hierarchy
Init docstring:
Construct a new Legend object

Parameters
----------
arg
    dict of properties compatible with this constructor or
    an instance of plotly.graph_objs.layout.Legend
bgcolor
    Sets the legend background color.
bordercolor
    Sets the color of the border enclosing the legend.
borderwidth
    Sets the width (in px) of the border enclosing the
    legend.
font
    Sets the font used to text the legend items.
orientation
    Sets the orientation of the legend.
tracegroupgap
    Sets the amount of vertical space (in px) between
    legend groups.
traceorder
    Determines the order at which the legend items are
    displayed. If "normal", the items are displayed top-to-
    bottom in the same order as the input data. If
    "reversed", the items are displayed in the opposite
    order as "normal". If "grouped", the items are
    displayed in groups (when a trace `legendgroup` is
    provided). if "grouped+reversed", the items are
    displayed in the opposite order as "grouped".
uirevision
    Controls persistence of legend-driven changes in trace
    and pie label visibility. Defaults to
    `layout.uirevision`.
valign
    Sets the vertical alignment of the symbols with respect
    to their associated text.
x
    Sets the x position (in normalized coordinates) of the
    legend.
xanchor
    Sets the legend's horizontal position anchor. This
    anchor binds the `x` position to the "left", "center"
    or "right" of the legend.
y
    Sets the y position (in normalized coordinates) of the
    legend.
yanchor
    Sets the legend's vertical position anchor This anchor
    binds the `y` position to the "top", "middle" or
    "bottom" of the legend.

Returns
-------
Legend

So the x and y values are normalized coordinates where x=0, y=0 is at the bottom left of the chart and x=1, y=1 is at the top right. You can go a little above 1 and a little below 0 to push the legend into the margin outside of the plotting area.

Hope that helps!
-Jon

1 Like

Here is what I did:

traces1 = []

for i in range(0,100):
traces1.append(go.Bar(
x = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9],
y = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9],
name = β€˜column’ + str(i)
))

Important

  • Use y close to zero (0)
  • Use yanchor
  • Increase bottom margin using b=200

layout0 = go.Layout(
width=1000, height=1000,
legend=dict(orientation=β€œh”, x = 0,y = -0.2, yanchor=β€œtop”),
margin=go.layout.Margin(b=200)
)

fig1 = go.Figure(data=traces1, layout=layout0)

offline.iplot(fig1)

This produces a well behaved plot:

deleted the top of the graph

1 Like