How to change figure size in Plotly in Python

To change the figure size in plotly.py, we can manipulate the graph size and the margin dimensions. We can achieve the required change in the graph through different methods for different use cases.

Change Height, Width, & Margins with Plotly Express in Python

import plotly.express as px

df = px.data.wind()
fig = px.scatter(df, x="direction", y="frequency",
                 width=800, height=400)

fig.show()

Change Height, Width, & Margins with Plotly Express in Python

Here we have used Plotly Express to illustrate an example. Plotly Express is the high-level interface to Plotly operating on various data types. It produces the same interactive charts with minimal codes.

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="sex",
                 width=800, height=400)

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

fig.show()

Change Height, Width, & Margins with Plotly Express in Python

Here we have used width and height as function arguments in the scatter chart method of Plotly Express. We have used these arguments to set the dimension of the chart area. To update the margins of the graph we have used margin dimensions, left, right top, and bottom (l, r, t, b) in dictionary format. We have passed the margin parameters as an argument to the update_layout() method called on the fig object. By doing this, we have adjusted the graph objects in the chart area to render a better visualization.

Change Height, Width, & Margins with Graph Objects in plotly

import plotly.graph_objects as go

fig = go.Figure()

df = px.data.wind()
fig.add_trace(go.Scatter(
    x= df["direction"],
    y= df["frequency"]
))

fig.show()

Change Height, Width, & Margins with Graph Objects in plotly

For greater control over the chart elements, we can use Graph objects instead of Plotly Express. Graph objects are the building blocks of figures at the low level.

import plotly.graph_objects as go

fig = go.Figure()

df = px.data.wind()
fig.add_trace(go.Scatter(
    x= df["direction"],
    y= df["frequency"]
))

fig.update_layout(
    title="Wind Frequencies",
    xaxis_title="Direction",
    yaxis_title="Frequency",
    autosize=False,
    width=600,
    height=400,
    margin=dict(
        l=50,
        r=50,
        b=50,
        t=50,
        pad=4
    ),
    paper_bgcolor="LightSteelBlue",
)

fig.show()

Change Height, Width, & Margins with Graph Objects in plotly

Here again, we have used width and height to set the chart area, and margin arguments to adjust the graph objects. But this time we have used all these inside the update_layout() function of the fig object. And we have got the desired output.

Automatically changing Margins in plotly

import plotly.graph_objects as go


fig = go.Figure()

fig.add_trace(go.Bar(
    x=["Coral", "Turquoise", "Cornflowerblue", "Khaki"],
    y=[6, 4, 2, 8]
))

fig.update_layout(
    autosize=False,
    width=600,
    height=500,
    yaxis=dict(
        title_text="Y-axis Title",
        ticktext=["Very long title", "long title", "5", "short title"],
        tickvals=[2, 4, 6, 8],
        tickmode="array",
        titlefont=dict(size=20),
    )
)

fig.update_yaxes(automargin=False)

fig.show()

Automatically changing Margins in plotly

import plotly.graph_objects as go


fig = go.Figure()

fig.add_trace(go.Bar(
    x=["Coral", "Turquoise", "Cornflowerblue", "Khaki"],
    y=[6, 4, 2, 8]
))

fig.update_layout(
    autosize=False,
    width=600,
    height=500,
    yaxis=dict(
        title_text="Y-axis Title",
        ticktext=["Very long title", "long title", "5", "short title"],
        tickvals=[2, 4, 6, 8],
        tickmode="array",
        titlefont=dict(size=20),
    )
)

fig.update_yaxes(automargin=True)

fig.show()

How to change figure size in Plotly in Python

In this case, we have also updated the y-axis elements of the graph to make the y-axis tick labels a bit lengthier. Without margin manipulation, the y-axis tick labels would overlap with the y-axis title. To avoid this overlapping, we have set automargin to True which will automatically adjust the margin size. And automargin parameter will avoid tick labels from overlapping with the axis titles.

Read More: Seaborn Multiple Line Plot in Python

Leave a Reply

Your email address will not be published.