绘图 scatter3d 方面模式跨轴不一致

问题描述 投票:0回答:1

下面是我的情节图,可以在这里看到。我试图让我的图像显示为轴范围内所述的 16x9 纵横比。然而,当我将

aspectmode
设置为
data
时,我得到了一个极其拉长的图,它似乎与正确的纵横比不匹配。

fig = go.Figure(
    data = go.Scatter3d(
        x=df['frame'], 
        y=df['x'], 
        z=df['y'],
        mode='markers',
        marker=dict(
            color=clusters.labels_,
            colorscale="Cividis",
            opacity=0.8
        )
    )
)
fig['layout']['scene']['aspectmode'] = "data"
fig['layout']['scene']['zaxis']['range'] = [1080, 0]
fig['layout']['scene']['yaxis']['range'] = [0, 1920]
fig.show()
python plotly
1个回答
0
投票

这主要是由 empet 在 Plotly 社区论坛 中回答的,但我将在这里建立他们的解决方案,因为该线程没有解决。

据我所知,您需要设置 2 个属性:

aspectmode
需要设置为
manual
,您可以在其中设置轴限制(以便 Plotly 不会为您调整轴的大小),以及
aspectratio 
(设置为字典)确定缩放比例。

快速回答是

fig['layout']['scene']['aspectmode'] = "manual"
fig['layout']['scene']['zaxis']['range'] = [1080, 0]
fig['layout']['scene']['yaxis']['range'] = [0, 1920]
fig['layout']['scene']['aspectratio'] = dict(x=1, y=16, z=9)

您还可以使用

update_layout
函数设置器执行此操作,如本示例所示:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

# Generate random data
df = pd.DataFrame({
    'x': np.random.rand(100) * 1400,
    'y': np.random.rand(100) * 2000,
    'z': np.random.rand(100) * 200 + 500
})

fig = go.Figure(
    data = go.Scatter3d(
        x=df['x'], 
        y=df['y'], 
        z=df['z'],
        mode='markers',
    )
)

fig.update_layout(scene = dict(
     yaxis = dict(range=[0,2000],),
     zaxis = dict(range=[0,1000],),
     aspectmode='manual',
     aspectratio=dict(x=1, y=16, z=9)))

fig.show()

这会产生下图:

© www.soinside.com 2019 - 2024. All rights reserved.