You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly 3D图表坐标轴显示问题:如何显示轴线条及箭头

Plotly 3D图表坐标轴线条显示与箭头添加问题解决

问题说明

使用Plotly绘制3D散点图时,尝试用fig.update_xaxes(showline=True, linewidth=2, linecolor='black')设置坐标轴线条无效果,同时需要为3D坐标轴添加箭头。

解决方法

Plotly的3D图表坐标轴配置逻辑和2D不同,不能直接用update_xaxes(),需要通过fig.update_layout()中的scene参数来配置x/y/z轴:

  • 开启并设置坐标轴线条:在scene的xaxis/yaxis/zaxis中设置showline=True,同时指定linewidth和linecolor
  • 添加坐标轴箭头:在对应轴的配置中设置showarrow=True,还可按需调整arrowwidth、arrowcolor等属性

修改后的完整代码

import plotly.express as px
import numpy as np
from random import randint as rint
import pandas as pd

headers = ("Rank", "GMV", "Inventory", "Categories", "ETRS")
sellers = [(rint(20, 100), rint(500, 10000), rint(10, 200), rint(1, 6), rint(0, 1) == 1) \
           for _ in range(100)]

df = pd.DataFrame(sellers, columns=headers)

fig = px.scatter_3d(df, x='Inventory', y='Rank', z='GMV', size='Categories', color='ETRS')

# 配置3D场景的坐标轴样式
fig.update_layout(
    scene=dict(
        xaxis=dict(
            showline=True,
            linewidth=2,
            linecolor='black',
            showarrow=True,
            arrowwidth=3,
            arrowcolor='black'
        ),
        yaxis=dict(
            showline=True,
            linewidth=2,
            linecolor='black',
            showarrow=True,
            arrowwidth=3,
            arrowcolor='black'
        ),
        zaxis=dict(
            showline=True,
            linewidth=2,
            linecolor='black',
            showarrow=True,
            arrowwidth=3,
            arrowcolor='black'
        )
    )
)

fig.show()

关键配置说明

  • scene是Plotly 3D图表的核心配置区域,所有3D坐标轴、背景等设置都在这里定义
  • 每个轴的showline控制是否显示轴线,linewidth/linecolor设置线条样式
  • showarrow开启箭头显示,arrowwidth/arrowcolor调整箭头外观

内容的提问来源于stack exchange,提问作者khachik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 00:19:53