Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add space between the tick labels and the graph in plotly (python)?

Tags:

python

plotly

If I create a horizontal bar graph using plotly, the labels for each bar are right up against the graph. I'd like to add some space/pad/margin between the label and the graph. How can I do this?

Example:

import plotly.offline as py
import plotly.graph_objs as go
labels = ['Alice','Bob','Carl']
vals = [2,5,4]

data = [go.Bar(x=vals, y=labels, orientation='h')]

fig = go.Figure(data)
py.iplot(fig)

enter image description here

like image 868
jpobst Avatar asked Mar 04 '26 08:03

jpobst


1 Answers

Just use parameter pad in margin. Check example from docs here. Code:

import plotly.offline as py
import plotly.graph_objs as go

labels = ['Alice','Bob','Carl']
vals = [2,5,4]

data = [go.Bar(x=vals, y=labels, orientation='h')]

layout = go.Layout(
    margin=dict(
        pad=20
    ),
    title = 'hbar',
)
fig = go.Figure(data=data,layout=layout)

py.plot(fig, filename='horizontal-bar.html')

And plot should be looks something like that: Your plot

like image 96
Dmitriy Kisil Avatar answered Mar 05 '26 22:03

Dmitriy Kisil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!