Plotly:在轴刻度中隐藏日期,但在使用“x unified”创建的悬停标签标题中显示

Plotly: Hide date in axis ticks but show in hoverlabel title created with `x unified`

在轴刻度标签中,我只想显示月份和年份(分组),就像这样 -

但这也意味着我必须牺牲 hoverlabel 标题中的日期 (1-31)。有没有一种方法可以隐藏轴上日期的日期部分(分组年份和短月份)并在悬停标签标题上看到它?类似这样,但轴上每个月之前没有 01s。

这是重现图形的代码-

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y=['GOOG','AAPL'])
fig.update_layout(hovermode='x unified', xaxis_title=None, yaxis_title=None, xaxis_tickformat='%d%b\n%Y',
                 hoverlabel=dict(namelength=0))
fig.update_traces(hovertemplate=None)
# fig.update_traces(hovertemplate='%{x|%d%b,%Y} value: %{y:.2f}')

可以修改update_layout方法中的参数xaxis_hoverformat。在这种情况下,您不需要使用悬停模板。

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y=['GOOG','AAPL'])
fig.update_layout(
    hovermode='x unified', 
    xaxis_title=None, 
    yaxis_title=None, 
    xaxis_tickformat='%b\n%Y',
    xaxis_hoverformat='%d%b,%Y',
    hoverlabel=dict(namelength=0)
)

fig.update_traces(hovertemplate=None)
fig.show()