jpobst
jpobst

Reputation: 3701

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

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

Upvotes: 11

Views: 12409

Answers (3)

HeyMan
HeyMan

Reputation: 1845

Shorter solution:

fig.update_layout(margin_pad=10)

Upvotes: 8

Dmitriy Kisil
Dmitriy Kisil

Reputation: 2998

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

Upvotes: 23

tianhua liao
tianhua liao

Reputation: 675

I think you could add some code like this.

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 = dict(yaxis=dict(ticksuffix="   "))
fig = go.Figure(data=data,layout=layout)
py.iplot(fig)

add a suffix will fix this problem easily. I have checked the reference plotly ref, it also have more suitable key named tickformat, but it hard to use so I didn't use it.

Upvotes: 3

Related Questions