Search code examples
pythonplotlyplotly-pythonfigure

How to change plotly figure size


I made the following scatter plot with Plotly:

enter image description here

import plotly
import plotly.plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go


trace1 = go.Scatter(
    x=x1_tsne,  # x-coordinates of trace
    y=y1_tsne,  # y-coordinates of trace
    mode="markers ",  # scatter mode (more in UG section 1)
    text=label3,
    opacity=1,
    textposition="top center",
    marker=dict(size=25, color=color_4, symbol=marker_list_2, line=dict(width=0.5)),
    textfont=dict(
        color="black",
        size=18,  # can change the size of font here
        family="Times New Roman",
    ),
)

layout = {
    "xaxis": {
        "showticklabels": False,
        "showgrid": False,
        "zeroline": False,
        "linecolor": "black",
        "linewidth": 2,
        "mirror": True,
        "autorange": False,
        "range": [-40, 40],
    },
    "yaxis": {
        "showticklabels": False,
        "showgrid": False,
        "zeroline": False,
        "linecolor": "black",
        "linewidth": 2,
        "mirror": True,
        "autorange": False,
        "range": [-40, 40],
    },
}

data = [trace1]

fig = go.Figure(data=data, layout=layout)

py.iplot(fig)

I want the height, width, and the markers to be like the plot shown below (made in Matplotlib):

enter image description here

I tried tuning the range, and using Autorange, but they did not help.


EDIT: The following code worked for me:

trace1 = go.Scatter(
    x=x1_tsne,  # x-coordinates of trace
    y=y1_tsne,  # y-coordinates of trace
    mode="markers +text ",  # scatter mode (more in UG section 1)
    text=label3,
    opacity=1,
    textposition="top center",
    marker=dict(size=25, color=color_4, symbol=marker_list_2, line=dict(width=0.5)),
    textfont=dict(
        color="black",
        size=18,  # can change the size of font here
        family="Times New Roman",
    ),
)
data = [trace1]

layout = go.Layout(
    autosize=False,
    width=1000,
    height=1000,
    xaxis=go.layout.XAxis(linecolor="black", linewidth=1, mirror=True),
    yaxis=go.layout.YAxis(linecolor="black", linewidth=1, mirror=True),
    margin=go.layout.Margin(l=50, r=50, b=100, t=100, pad=4),
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename="size-margins")

Solution

  • Consider using:

    fig.update_layout(
        autosize=False,
        width=800,
        height=800,
    )
    

    ...and then eventually reduce the size of your marker.

    Full code:

    import plotly.graph_objs as go
    
    trace1 = go.Scatter(
        x=x1_tsne,  # x-coordinates of trace
        y=y1_tsne,  # y-coordinates of trace
        mode="markers +text ",  # scatter mode (more in UG section 1)
        text=label3,
        opacity=1,
        textposition="top center",
        marker=dict(size=12, color=color_4, symbol=marker_list_2, line=dict(width=0.5)),
        textfont=dict(
            color="black",
            size=18,  # can change the size of font here
            family="Times New Roman",
        ),
    )
    data = [trace1]
    
    layout = go.Layout(
        autosize=False,
        width=1000,
        height=1000,
        xaxis=go.layout.XAxis(linecolor="black", linewidth=1, mirror=True),
        yaxis=go.layout.YAxis(linecolor="black", linewidth=1, mirror=True),
        margin=go.layout.Margin(l=50, r=50, b=100, t=100, pad=4),
    )
    
    fig = go.Figure(data=data, layout=layout)