Search code examples
pythonplotlyplotly-python

how to extend background size in plotly


I have added text-box beside to scatter plot.

Since figure size is defined, I can not see text-box proparly. I would like to extend background and put text-box beside to scatter plot.

import plotly.graph_objects as go
import plotly.express as px


config = {'staticPlot': True}
fig = go.Figure()
fig = px.scatter(terms, x='pr_auc_x1', y='pr_auc_x2', color='hue2', title='test')
fig.add_shape( type="line", x0=0, y0=0, x1=1,y1=1, line=dict(color="Grey",width=1) )


fig.update_layout(
        annotations=[
            go.layout.Annotation(
                text="..<br>".join([i for i in terms[terms.hue == 1].legend]),
                align='left',
                showarrow=False,
                xref='paper',
                yref='paper',
                x=1.22,
                y=0.93,
                bordercolor='black',
                borderwidth=1,
            )
        ]
   
)

fig.show(config=config)

enter image description here


Solution

  • In this case, conversely, the graph area can be narrowed and a legend and text box can be placed. Increasing the graph size will do the same thing as long as the position of the x-axis, where each position is set, remains the same. You can solve this problem by setting a domain where you can set the graph area, placing a legend to the left of it, and then placing a text box further to the left. I don't have similar sample data, so I used general iris data to fit your example.

    import plotly.graph_objects as go
    import plotly.express as px
    
    config = {'staticPlot': True}
    fig = go.Figure()
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title='test')
    fig.add_shape( type="line", x0=0, y0=0, x1=df.sepal_width.max(), y1=df.sepal_length.max(), line=dict(color="Grey",width=1) )
    
    fig.update_layout(
        height=500,
        xaxis=dict(domain = [0, 0.75],  anchor = 'y1'),
        legend=dict(
            yanchor="top",
            y=0.99,
            xanchor="right",
            x=0.87),
            annotations=[
                go.layout.Annotation(
                    text="..<br>".join([i for i in df.species[:20]]),
                    align='left',
                    showarrow=False,
                    xref='paper',
                    yref='paper',
                    x=0.95,
                    y=0.99,
                    bordercolor='black',
                    borderwidth=1,
                )
            ]
    )
    
    fig.show(config=config)
    

    enter image description here