Plotly:轴刻度和细节调整

Plotly: axis scale and details adjustment

我正在绘制一系列值,这些值与 1 月到 10 月范围内的每月测量值相关联。我 post 我的代码就在这里(虽然我不能共享数据框)。 所以,问题是这样的:正如您从 picture 中看到的那样,当我用鼠标悬停时它显示月份(在本例中为 Jan),这是正确的。但是通过查看 x 轴,这些条形似乎与 2 月相关联。所以基本上我需要“重新缩放”。我尝试使用带有 x 轴的 nticks 参数,但如果我增加它开始显示我不需要的周数(例如通过设置 nticks = 35 它显示月份之间的中间日期数(例如 1 月 17 日写在 1 月和 2 月之间)). 有什么办法可以解决这个问题吗?提前致谢!

fig = go.Figure()
fig.update_layout(
    title='Energia Consumata VS Energia Prelevata VS Energia Immessa VS Energia Autoconsumata VS Energia Prodotta',
    xaxis_tickfont_size=14,
    xaxis = dict(
        showgrid =True,
        zeroline = True, 
        nticks = 20, 
        showline = True,
        title='Tempo',
        titlefont_size=16,
        tickfont_size=14,
    ),
    yaxis=dict(
        showgrid =True,
        zeroline = True, 
        nticks = 20, 
        showline = True,
        title='kWh',
        titlefont_size=16,
        tickfont_size=14,
    ),
    barmode='group',
    bargap=0, # gap between bars of adjacent location coordinates.
    bargroupgap=0 # gap between bars of the same location coordinate.
)
#Totale fabbisogno della comunità
fig.add_trace(go.Bar(x=timeframe, y=df_comunita.Consumo,
                base=0,
                marker_color='red',
                name='Energia Consumata Totale'
                ))
#Totale Prelevata dalla Rete
fig.add_trace(go.Bar(x=timeframe, y=df_comunita.Energia_Attiva_Ingresso_Delta,
                base=0,
                marker_color='#ff8000',
                name='Energia Prelevata dalla Rete'
                ))
#Totale Prodotta
fig.add_trace(go.Bar(x=timeframe, y=df_comunita.Produzione_Impianti_Associati,
                base=0,
                marker_color='yellow',
                name='Energia Prodotta'
                ))
#Totale Immessa in Rete
fig.add_trace(go.Bar(x=timeframe, y=df_comunita.Energia_Attiva_Uscita_Delta,
                base=0,
                marker_color='green',
                name='Energia Immessa in Rete'
                ))
#Totale Autoconsumata
fig.add_trace(go.Bar(x=timeframe, y=df_comunita.Energia_Autoconsumata,
                base=0,
                marker_color='#2600ff',
                name='Energia Autoconsumata'
                ))
#Consumo medio CER
fig.add_trace(go.Scatter(x=df_comunita.GG_ore_minuti, y = df_comunita['Consumo'],
                line = dict(color = 'black', width=0.5), 
                name = "Consumo comunità"))
fig.update_xaxes(rangeslider_visible=True)
fig.show()

我通过在指定 x 轴频率的绘图之前添加这些代码行来解决:

fig.layout.xaxis.tickvals = pd.date_range(min(df_comunita.GG_ore_minuti), max(df_comunita.GG_ore_minuti), freq='1M')
fig.layout.xaxis.tickformat = '%b'