Center plotly figure

Is there a easy way for me to center my figure? When I use jupyter-nbconvert to convert to html, the figure is aligned on the right.

My code looks like this

fig = make_subplots(specs=[[{'secondary_y': True}]])    # I WANT TO CENTER THIS FIGURE
 
fig.add_trace(go.Bar(...))
 
fig.add_trace(go.Scatter...(),
              secondary_y="True")

fig.update_layout(
    title='My Plot', 
    barmode='overlay', 
    plot_bgcolor='rgb(245,245,245)',
    legend=dict(
        x=0.02,
        y=0.95),
    margin=dict(t=0, b=0, l=0, r=0)
)

Thanks

1 Like

Hi @tud7 welcome to the forum! Could you please attach screenshots to show what the problem is? With the zero margin which you are setting the figure (ie the axes domain) should be centered.

Thank you!!!

You can see the plot is all the way to the left compared to the markdown. The markdown text is right-aligned.
It looks fine inside jupyter-notebook, but not in the generated html

I run the following command to convert to html:

jupyter-nbconvert --execute --no-input $IPYNB_FILE

-----

Hi @tud7

Please see the link from this old answer (2017): Centering Plotly Output on Jupyter Notebook. I’m not sure if this method is still valid.

Before I post this question, I saw that… but unfortunately, it does not work. I think the css class has changed.

Hi @tud7,

In this case it’s better to address your question on Jupyter discourse, section Q&A:
https://discourse.jupyter.org/c/questions/18.

1 Like