Search code examples
pythonplotlyaxes

Plotly: axis scale and details adjustment


I'm plotting a series of values which are associated to monthly measurements in a range from January to October. I post my code just down here (I can't share the dataframe though). So, the problem is this: as you can see from the picture, when I hoover with the mouse it shows the month (Jan in this case), and that's right. But by looking at the x axis it looks like that those bars are associated with Feb. So basically I need to "rescale". I tried with the nticks parameter with the x axis, but If I increase it starts to show weeks that I don't need (e.g. by putting nticks = 35 it shows the middle day number between months (e.g. 17 Jan written between Jan and Feb)). Is there a way I can solve this? Thanks in advance!

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

Solution

  • I solved by adding these lines of code before the plotting that specify the frequency of the x axis:

    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'