简体   繁体   中英

Plotly: Hide date in axis ticks but show in hoverlabel

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. 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? Something like this but without the 01 s before each month on the axis.

在此处输入图像描述

You can recreate this graph by copying this code-

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y='GOOG')
fig.update_layout(xaxis_title=None, yaxis_title=None, xaxis_tickformat='%d%b\n%Y')

This should be very possible. You can start out with the following and fine-tune it from there:

fig.update_traces(hovertemplate="%{x|%d%b,%Y} value: %{y:.2f}")

在此处输入图像描述

Complete code:

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y='GOOG')
fig.update_layout(xaxis_title=None, yaxis_title=None)

fig.update_traces(hovertemplate="%{x|%d%b,%Y} value: %{y:.2f}")
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