简体   繁体   中英

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

In the axis ticklabels, I want to show only the month and the year (grouped), like this -

在此处输入图像描述

But that also means I have to sacrifice on the day of the date (1-31) in the hoverlabel title. Is there a way I can hide the day part of the date on the axis (with grouped years and short month) and yet see it on the hoverlabel title? Something like this but without the 01 s before each month on the axis.

在此处输入图像描述

Here's the code to reproduce the graph-

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}')

You can modify the parameter xaxis_hoverformat in the update_layout method. In this case, you don't need to use a hovertemplate.

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM