Search code examples
pythonplotlydata-visualizationplotly-dashplotly-python

How to change the x-axis and y-axis labels in plotly?


How can I change the x and y-axis labels in plotly because in matplotlib, I can simply use plt.xlabel but I am unable to do that in plotly.

By using this code in a dataframe:

Date = df[df.Country=="India"].Date
New_cases = df[df.Country=="India"]['7day_rolling_avg']

px.line(df,x=Date, y=New_cases, title="India Daily New Covid Cases")

I get this output:

I get this output:

In this X and Y axis are labeled as X and Y how can I change the name of X and Y axis to "Date" and "Cases"


Solution

    • simple case of setting axis title
    update_layout(
        xaxis_title="Date", yaxis_title="7 day avg"
    )
    

    full code as MWE

    import pandas as pd
    import io, requests
    
    df = pd.read_csv(
        io.StringIO(
            requests.get(
                "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv"
            ).text
        )
    )
    df["Date"] = pd.to_datetime(df["date"])
    df["Country"] = df["location"]
    df["7day_rolling_avg"] = df["daily_people_vaccinated_per_hundred"]
    
    Date = df[df.Country == "India"].Date
    New_cases = df[df.Country == "India"]["7day_rolling_avg"]
    
    px.line(df, x=Date, y=New_cases, title="India Daily New Covid Cases").update_layout(
        xaxis_title="Date", yaxis_title="7 day avg"
    )
    

    enter image description here