vasili111
vasili111

Reputation: 6940

Plotly: How to prevent title from overlapping the plot?

Here is code:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    name='Group 1',
    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
fig.add_trace(go.Bar(
    name='Group 2',
    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
fig.update_layout(barmode='group',
                  title=dict(
                            text = "Some <br> title <br> here",
                            x = 0.5,
                            y = 0.95,
                            xanchor =  'center',
                            yanchor = 'top',
                            #pad = dict(
                            #            t = 0
                            #           ),
                            font = dict(
                                        #family='Courier New, monospace',
                                        size = 40,
                                        #color='#000000'
                                        )
                            ))


fig.show()

This is output:

enter image description here

Question: How to properly align title here without reducing plot title font size?

Of course this is just example and actual plot will look differently. Main idea of the question is to get the solution for proper aliment of titles with big size of the font.

Upvotes: 5

Views: 6701

Answers (1)

vestland
vestland

Reputation: 61204

An adjustment of the margins along with a small adjustment of y=0.95 should do the trick:

enter image description here

Complete code:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    name='Group 1',
    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
fig.add_trace(go.Bar(
    name='Group 2',
    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
fig.update_layout(barmode='group',
                  title=dict(
                            text = "Some <br> title <br> here",
                            x = 0.5,
                            y = 0.90,
                            xanchor =  'center',
                            yanchor = 'top',
                            #pad = dict(
                            #            t = 0
                            #           ),
                            font = dict(
                                        #family='Courier New, monospace',
                                        size = 40,
                                        #color='#000000'
                                        )
                            ))

fig.update_layout(margin=dict(l=50, r=50, t=200, b=50),paper_bgcolor="LightSteelBlue")
fig.show()

Upvotes: 5

Related Questions