Plotly:如何更改 x 轴上的默认日期并从轴上删除年份?

Plotly: How to change default date on x-axis and remove year from axis?

我正在通过 plotly 创建折线图。我 运行 遇到两个问题。 Plotly 默认在 x 轴上每隔 7 个值显示一次,它显示我的第一个 x 轴值的年份值(如屏幕截图所示)。这是代码运行。如果其中一个可以修复,那就太好了!

from connect_to_db import get_df
import plotly.express as px
import os

df = get_df()

fig = px.bar(df, x='Date', y='Total', color='CUSTOMER_TYPE',
             color_discrete_sequence=["#E15759","#8CD17D"],  width=800, height=400)

fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)', 'paper_bgcolor': 'rgba(0, 0, 0, 0)', })

fig.update_layout(
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.02,
        xanchor="right",
        x=.4))
fig.update_layout({
    'legend_title_text': ''
},
    xaxis_title="",
    yaxis_title="Tested Count"

)
fig.update_xaxes(showline=True, linewidth=1, linecolor='#FBFBFB', mirror=False)
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor="#FBFBFB",showline=True, linewidth=2, linecolor='#FBFBFB', mirror=False)
fig.show()

谢谢大家!

为了使年份消失,您只需为 xaxis_tickformat 制定另一个规范,例如 xaxis_tickformat = '%b' 以获得:

或者您可以选择 xaxis_tickformat = '%B' 并获得:

根据您的要求,在这两种情况下都删除了年份。

完整代码:

import plotly.graph_objects as go

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

fig = go.Figure(go.Scatter(
    x = df['Date'],
    y = df['AAPL.High'],
))

fig.update_layout(
    title = 'Time Series with Custom Date-Time Format',
    #xaxis_tickformat = '%d %B (%a)<br>%Y'
    xaxis_tickformat = '%d %B <br>%Y'
    #xaxis_tickformat = '%B'

)

fig.show()