57

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') 
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
hemanta
  • 1,405
  • 2
  • 13
  • 23

1 Answers1

90

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)

rpanai
  • 12,515
  • 2
  • 42
  • 64
  • 1
    When I add this before py.iplot, it says figure object has no attribute 'update_layout'. – hemanta Jan 28 '20 at 18:41
  • 1
    Which `plotly` version are you using? Try to add the same values to your `layout` dict. You could eventually use `layout["autosize"] = False` and so on just after defining layout. – rpanai Jan 28 '20 at 18:45
  • 2
    Its version 3.6. With your suggestions, I am able to do it in this version of plotly. Thank you. I tried this before for an interactive plot only. – hemanta Jan 28 '20 at 19:11
  • 1
    Marker size are still the same. – hemanta Jan 28 '20 at 19:13
  • 1
    Even is after trace1, it worked, is that because I am using this layout defined in go.Figure? – hemanta Jan 28 '20 at 19:17
  • 1
    I'd suggest you to move to version 4.5 they changed few things and it will be easier to use documentation. – rpanai Jan 28 '20 at 19:21