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

拈花ヽ惹草 提交于 2021-01-04 03:16:46

问题


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)


回答1:


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:




回答2:


Shorter solution:

fig.update_layout(margin_pad=10)



回答3:


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.



来源:https://stackoverflow.com/questions/52391451/how-do-i-add-space-between-the-tick-labels-and-the-graph-in-plotly-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!