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

如何移除Plotly三元图(Ternary Plot)角落的0刻度

移除三元图角落的0刻度

我用以下代码生成了三元图,希望去掉图中三个角落的0刻度:

import plotly.graph_objects as go
import plotly.express as px  

fig = go.Figure(go.Scatterternary({
    'mode': 'markers', 'a': [0.3],'b': [0.5], 'c': [0.6],
    'marker': {'color': 'AliceBlue','size': 14,'line': {'width': 2} },
                                  }))
fig.update_layout({
    'ternary': {
        'sum': 100,
        'aaxis': {'nticks':1, 'ticks':""},
        'baxis': {'nticks':1},
        'caxis': {'nticks':1}  }})

fig.update_traces( hovertemplate = "<b>CatA: %{a:.0f}<br>CatB: %{b:.0f}<br>CatC: %{c:.0f}<extra></extra>")

fig.show()

当前图表效果:
移除0刻度

但Plotly官方文档显示nticks的最小值为1(设置为0无效),请问该怎么移除这些角落的0刻度?
文档说明截图:
文档截图

解决方法

虽然nticks无法设为0,但可以通过两种简单方式移除0刻度:

方法1:隐藏刻度标签

给每个坐标轴添加showticklabels=False配置,直接隐藏所有刻度文本:

import plotly.graph_objects as go
import plotly.express as px  

fig = go.Figure(go.Scatterternary({
    'mode': 'markers', 'a': [0.3],'b': [0.5], 'c': [0.6],
    'marker': {'color': 'AliceBlue','size': 14,'line': {'width': 2} },
                                  }))
fig.update_layout({
    'ternary': {
        'sum': 100,
        'aaxis': {'nticks':1, 'ticks':"", 'showticklabels': False},
        'baxis': {'nticks':1, 'showticklabels': False},
        'caxis': {'nticks':1, 'showticklabels': False}  }})

fig.update_traces( hovertemplate = "<b>CatA: %{a:.0f}<br>CatB: %{b:.0f}<br>CatC: %{c:.0f}<extra></extra>")

fig.show()

方法2:自定义空刻度数组

通过tickmode='array'指定空的刻度值数组,让Plotly不渲染任何刻度:

import plotly.graph_objects as go
import plotly.express as px  

fig = go.Figure(go.Scatterternary({
    'mode': 'markers', 'a': [0.3],'b': [0.5], 'c': [0.6],
    'marker': {'color': 'AliceBlue','size': 14,'line': {'width': 2} },
                                  }))
fig.update_layout({
    'ternary': {
        'sum': 100,
        'aaxis': {'nticks':1, 'ticks':"", 'tickmode': 'array', 'tickvals': []},
        'baxis': {'nticks':1, 'tickmode': 'array', 'tickvals': []},
        'caxis': {'nticks':1, 'tickmode': 'array', 'tickvals': []}  }})

fig.update_traces( hovertemplate = "<b>CatA: %{a:.0f}<br>CatB: %{b:.0f}<br>CatC: %{c:.0f}<extra></extra>")

fig.show()

两种方法都能彻底移除角落的0刻度,方法1操作更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:25:12