Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to change figure size?

enter image description hereI have made a scatter plot using matplotlib and Plotly. I want the height, width and the markers to be scatter as in matplotlib plot. Please see the attached plots.

I used the following code in Plotly

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][![enter image description here][1]][1] 
        
    },
    '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 try to tune the range but did not help. In addition, I use Autorange that did not help. Could you please help me with this.

I want the image as enter image description here

##update: This can be done by the following code. I am updating this in the question:

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') 
like image 755
hemanta Avatar asked Jan 28 '20 16:01

hemanta


People also ask

How can I increase my figure size in Plotly?

One of the best ways to modify the size of the Plotly figure is by adjusting the width and the height parameters. We will start by discussing how to change the height and width parameters using Plotly Express. For example, in the following code, we create a simple line chart using the default Plotly's dimensions.

How do you resize Plotly?

Automatically Adjust MarginsSet automargin=true (reference) and Plotly will automatically increase the margin size to prevent ticklabels from being cut off or overlapping with axis titles.

How do I make a graph bigger in Python?

pyplot library. To change the figure size, use figsize argument and set the width and the height of the plot.

Is Plotly customizable?

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments.


1 Answers

Have you considered to use

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

and eventually reduce the size of your marker?

UPDATE

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)

like image 193
rpanai Avatar answered Nov 15 '22 20:11

rpanai