在 Plotly 中隐藏轴上的 0 值

时间:2021-03-19 21:04:21

标签: python plotly

在 0 处有一个刻度标签是没有用的,而且会占用空间。我希望所有刻度标签都保持原样,除了我只想隐藏 x 和/或 y 轴上的 0 刻度标签。我该怎么做?

谢谢,

尼克

enter image description here

1 个答案:

答案 0 :(得分:0)

我怀疑我们使用的是不同的 Plotly 版本,并且这个装饰细节已经针对较新版本进行了改进。通过运行 px.data.tips() 使用 px.histogram(px.data.tips(), x = 'total_bill') 数据集制作直方图给了我这个图:

enter image description here

尝试一下,看看您是否得到了相同的结果。如果是这样,那么我目前唯一的建议是:


以下方法可能并不适合所有情况。诚然,这有点麻烦。但是,如果您可以使用它,我愿意解释所有细节。简而言之,该方法只是跳过 fig.update_xaxes(tickvals = ticksx) 中的零。这会改变这个示例图:

enter image description here

...进入这个:

enter image description here

完整代码:

import plotly.graph_objs as go
import plotly.express as px
import numpy as np

# px.histogram(px.data.tips(), x = 'total_bill', y = 'tip')

fig = go.Figure()
fig.add_traces(go.Scatter(x=[0,10,20,40], y = [40,20,10,0]))

figx = fig.data[0].x
figy = fig.data[0].y

ticksx = list(np.linspace(min(figx), max(figx), endpoint = True, num = 5))[1:]
ticksy = list(np.linspace(min(figy), max(figy), endpoint = True, num = 5))[1:]

fig.update_xaxes(tickvals = ticksx)
fig.update_yaxes(tickvals = ticksy)

fig.show()
相关问题