I have a Dash app with plots different data depending on the value selected in a dropdown. In one set of data, the series names are longer so the legend automatically repositions itself into the plot area.
The default behaviour is the legend has it’s own space to the right of the plot area, and as legend name sizes increase the plot area is shrunk accordingly to make room for the legend. This is until some minimum value is reached and plotly decides the legend should be superimposed on top of the plot area.
I never want the legend super imposed and want to keep it on the right. To solve this, ideally would be altering the minimum value rule I mentioned, or less ideally, by setting a fixed (smaller) size for the plot area to allow room to the right of the plot area for a legend with longer names. Is this possible?
I have produced a minimal dash app example below to demonstrate.
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div(children=[
# basic example adapted from Dash's getting started docs - demonstrates usable area within Div
html.Div([
dcc.Graph(
id='example-graph1',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'line', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'line', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization',
}
}
)
],
style={'width': 600, 'height': 600, 'border': '5px solid red'}
),
# basic example with long legend name - legend is auto-positioned and is trimmed to size of chart
html.Div([
dcc.Graph(
id='example-graph2',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'line', 'name': 'SFABCDEFGHIJKLMNOPQRSTUVWXYZ'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'line', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization',
}
}
)
],
style={'width': 600, 'height': 600, 'border': '5px solid red'}
),
# attempt to fix legend position
html.Div([
dcc.Graph(
id='example-graph3',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'line', 'name': 'SFABCDEFGHIJKLMNOPQRSTUVWXYZ'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'line', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization',
'autosize': False,
'legend': dict(orientation='v',yanchor='top',xanchor='right',y=1,x=-1),
'margin': dict(r=400)
}
}
)
],
style={'width': 600, 'height': 600, 'border': '5px solid red'}
),
])
if __name__ == '__main__':
app.run_server(debug=True)```