Using the 'automargin' property for bar graphs?


#1

Hello all,

I am generating graphs with:

f = {'data': [
                    {'x': df['x'], 'y': df['y'], 'text': df['y'], 'type': 'bar',
                        'name': 'Y', 'color': '#000099'},
                    {'x': df['x'], 'y': df['y2'], 'text': df['y2'], 'type': 'bar',
                        'name': 'Y2', 'color': '#800000'}
                    ],
            'layout': {
                'xaxis': {'autorange': True, 'title': 'X Axis', 'automargin': True, 'animate': True},
                'type': 'category',
                'title': 'My Title',
                'yaxis': {'autorange': True, 'title': 'Y Axis', 'automargin': True, 'animate': True}
                }
            }

dcc.Graph(id='my-graph', fig = f)

My x-labels are very long, and they get cut off by the x-axis title. Is there any reason why ‘automargin’ is not working?


#2

That should work. Could you paste a complete reproducable example? That is, just replace df['x'] and df['y'] with some hardcoded data with long labels


#3

Thanks for the quick reply - here you go:

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import numpy as np
app = dash.Dash(__name__)

label = ['This is a very super long label','This is also a ridiculously long label',
'This is a very super long label2','This is also a ridiculously long label2',
'This is a very super long label3','This is also a ridiculously long label3',
'This is a very super long label4','This is also a ridiculously long label4',
'This is a very super long label5','This is also a ridiculously long label5',
'This is a very super long label6','This is also a ridiculously long label6'
'This is a very super long label7','This is also a ridiculously long label7'
'This is a very super long label8','This is also a ridiculously long label8'
'This is a very super long label9','This is also a ridiculously long label9'
'This is a very super long label10','This is also a ridiculously long label10'
'This is a very super long label11','This is also a ridiculously long label11'
'This is a very super long label12','This is also a ridiculously long label12'
'This is a very super long label13','This is also a ridiculously long label13'
'This is a very super long label14','This is also a ridiculously long label14'
'This is a very super long label15','This is also a ridiculously long label15']

y = np.random.randint(100, size=len(label))
y2 = np.random.randint(100, size=len(label))

df = pd.DataFrame({'x':label,
                    'y':y,
                    'x2':label,
                    'y2':y2})

f = {'data': [
                    {'x': df['x'], 'y': df['y'], 'text': df['y'], 'type': 'bar',
                        'name': 'Y', 'color': '#000099'},
                    {'x': df['x'], 'y': df['y2'], 'text': df['y2'], 'type': 'bar',
                        'name': 'Y2', 'color': '#800000'}
                    ],
            'layout': {
                'xaxis': {'autorange': True, 'title': 'X Axis', 'automargin': True, 'animate': True},
                'type': 'category',
                'title': 'My Title',
                'yaxis': {'autorange': True, 'title': 'Y Axis', 'automargin': True, 'animate': True}
                }
            }

app.layout = html.Div(dcc.Graph(id='my-graph', figure = f))
if __name__ == '__main__':
    app.run_server(debug=True, host='0.0.0.0')

#4

Any suggestion on how to get this work?