I need help in adding text to my graph.
I have tried text = 'y' and text-position = 'inside' but the text goes vertical or gets squashed for small bar charts so it can fit inside the bar. I just want it to write across.
Here is a working example of the code that needs fixing:
app = dash.Dash(__name__)
app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})
labels1 = ['0-7', '8-12', '13-15', '16-20', '21-25', '26+']
values1 = [10, 30, 10, 5, 6, 8]
labels2 = ['India', 'Scotland', 'Germany', 'NW England', 'N Ireland', 'Norway', 'NE England', 'Paris', 'North Africa', 'scandinavia']
values2 = [1, 0, 4, 9, 11, 18, 50, 7, 0, 2]
values3 = [10, 111, 75, 20]
labels4 = ['Safety Manager', 'Office Administrator', 'Internal Officer', 'Assistant Producer']
bar_color = ['#f6fbfc', '#eef7fa', '#e6f3f7', '#deeff5', '#d6ebf2', '#cde7f0', '#c5e3ed', '#bddfeb', '#b5dbe8', '#add8e6']
bar_color2 = ['#e6f3f7', '#deeff5', '#d6ebf2', '#cde7f0', '#c5e3ed', '#bddfeb', '#b5dbe8', '#add8e6']
app.layout = html.Div([
html.Div([
html.Div([
dcc.Graph(id = 'age',
figure = {
'data': [go.Bar(x = values1,
y = labels1,
orientation = 'h',
marker=dict(color = bar_color2),
text = labels1,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per tenure',
yaxis=dict(
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
)
)
}
)
], className = 'four columns'),
html.Div([
dcc.Graph(id = 'location',
figure = {
'data': [go.Bar(x = values2,
y = labels2,
orientation = 'h',
marker=dict(color = bar_color),
text = labels2,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per region',
yaxis=dict(
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
) )
}
)
], className = 'four columns'),
html.Div([
dcc.Graph(id = 'job',
figure = {
'data': [go.Bar(x = values3,
y = labels4,
orientation = 'h',
marker=dict(color = bar_color2),
text = labels4,
textposition = 'inside'
)
],
'layout': go.Layout(title = 'Number of respondees per role',
yaxis=dict(
# automargin=True,
zeroline=False,
showline=False,
showgrid = False,
autorange="reversed",
),
xaxis=dict(
zeroline=False,
showline=False,
showgrid = False
)
)
}
)
], className = 'four columns')
], className = 'row')
])
if __name__ == '__main__':
app.run_server()
Here's the output:
Here's an example of how I want my text to look:

I need help with two things:
- Make the text align to the left not the right of the bar.
- If the bar length is short I want the text to still be visible (even if the bar length is zero) and not squashed or vertically aligned.
If you can also give an explanation of how to fix y-axis being cut off in the third chart that would be amazing. For now, I have to change the labels to force it to fit which is time-consuming. Is there a way of adding padding to the container or something?
Thanks.



