Search code examples
plotlyplotly-pythonr-plotlyggseasonplot

What is the python code corresponding to the following R code?


I have monthly sales for 5 years and I applied the following code In R to seasonal plot:

ggseasonplot(S5, year.labels=TRUE, year.labels.left=TRUE)

So how can I plot it using pythonic way?


Solution

  • import pandas as pd
    import io, requests
    import plotly.express as px
    
    # use requests so that there are no issues with utf-8 encoding
    df = pd.read_csv(
        io.StringIO(
            requests.get(
                "https://raw.githubusercontent.com/joaolcorreia/RFM-analysis/master/sample-orders.csv"
            ).text
        )
    )
    df["order_date"] = pd.to_datetime(df["order_date"])
    
    px.line(
        df.assign(
            year=df["order_date"].dt.year,
            month=df["order_date"].dt.month,
            month_name=df["order_date"].dt.strftime("%b"),
        )
        .groupby(["year", "month", "month_name"], as_index=False)
        .agg(value=("grand_total", "sum")),
        x="month_name",
        y="value",
        color="year",
    )
    

    enter image description here