Plotly:在轴刻度中隐藏日期但在 hoverlabel 中显示
Plotly: Hide date in axis ticks but show in hoverlabel
在轴刻度标签中,我只想显示月份和年份(分组),就像这样 -
但这也意味着我必须牺牲 hoverlabel 中日期 (1-31) 的日期。有没有一种方法可以隐藏轴上日期的日期部分(分组年份和短月份)并在悬停标签上看到它?类似这样,但轴上每个月之前没有 01
s。
您可以通过复制此代码重新创建此图表-
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')
这应该是很有可能的。您可以从以下开始并从那里进行微调:
fig.update_traces(hovertemplate="%{x|%d%b,%Y} value: %{y:.2f}")
完整代码:
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()
在轴刻度标签中,我只想显示月份和年份(分组),就像这样 -
但这也意味着我必须牺牲 hoverlabel 中日期 (1-31) 的日期。有没有一种方法可以隐藏轴上日期的日期部分(分组年份和短月份)并在悬停标签上看到它?类似这样,但轴上每个月之前没有 01
s。
您可以通过复制此代码重新创建此图表-
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')
这应该是很有可能的。您可以从以下开始并从那里进行微调:
fig.update_traces(hovertemplate="%{x|%d%b,%Y} value: %{y:.2f}")
完整代码:
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()