[Python] Python plotly express line chart with cumulative sum 2

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am new here (to post, search a lot); new to dash and plotly (second day) and my python is still limited.

    Well, that said. My problem... I have a simple dataframe from a cvs with date(%d-%m/%Y),value

    I wanted to have some graph, I have some, for that, because I am new, I added two "columns" one with the month and one other with the year (extracted from the date)

    df["date_year"] = pd.to_datetime(df['date'], format = '%d/%m/%Y').dt.strftime('%Y')
    df["date_months"] = pd.to_datetime(df['date'], format = '%d/%m/%Y').dt.strftime('%B')


    With that, I was able to "easily" produce a graph with the sum of value by month for a selected year (year selected via a dropdown)

    fig = px.histogram(df_filtered[mask], x='date_months', y='value', histfunc='sum'


    Probably not pretty, but working. What I can't have it is almost the same thing, but with a line by year

    I have something "near" with

    df_filtered["value_cumsum"] = df_filtered.groupby(['date_months','date_year'])['value'].cumsum()
    fig2 = px.line(df_filtered, x="date_months", y="value_cumsum", color='date_year')


    But... That's not it: There is all the data of each month, so lines produces a "saw". I want to have only 1 point by month, the last "value_cumsum".

    Do someone have an idea before I do more spaghetti code ?

    Regards, Me

    Continue reading...

Compartilhe esta Página