首页 > 解决方案 > Plotly:如何更改图形大小?

问题描述

在此处输入图像描述我使用 matplotlib 和 Plotly 制作了散点图。我希望像 matplotlib 图中那样分散高度、宽度和标记。请参阅附图。

我在 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)

我尝试调整范围但没有帮助。另外,我使用了没有帮助的自动量程。你能帮我解决这个问题吗?

我希望图像为 在此处输入图像描述

##update:这可以通过以下代码完成。我正在更新这个问题:

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') 

标签: pythonmatplotlibplotly

解决方案


你有没有考虑过使用

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

并最终减少size你的marker

更新

完整代码

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)


推荐阅读