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

2

30

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"

Flinn answered 30/1, 2022 at 16:31 Comment(2)
Have you tried implementing what the docs suggest?Epaminondas
Yes! update_layout worked, Thank you!Flinn
F
50
  • 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

Feodore answered 30/1, 2022 at 16:59 Comment(0)
K
4

Using the MWE shared, I have provided some improvements that might be helpful. Here, you can update many features including the title of both x and y axis. In addition, you can add traces to produce more user-friendly and interactive graphic.

import pandas as pd
import io, requests
import plotly.graph_objects as go

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"]

def updateFigure(fig):
    fig.update_traces(
        texttemplate='%{y:.2f}',
        textposition='top center',
        hovertemplate='Date: %{x}<br>Value: %{y:.2f}<br>',
        marker_line_color= '#800020',
        marker_line_width=1.5,
        opacity=0.8
    )

    fig.update_layout(
        autosize=False,
        width=800,
        height=400,
        template='plotly_dark',
        title=dict(
            text="India Daily New Covid Cases",
            font=dict(size=24, color='#FFFFFF'),
            x=0.5,
            y=0.9
        ),
        xaxis_title=dict(text='Date', font=dict(size=16, color='#FFFFFF')),
        yaxis_title=dict(text='7 day avg', font=dict(size=16, color='#FFFFFF')),
        plot_bgcolor='rgb(50, 50, 50)',
        xaxis=dict(tickfont=dict(size=14, color='#FFFFFF')),
        yaxis=dict(tickfont=dict(size=14, color='#FFFFFF')),
        legend=dict(x=0.1, y=1.1, orientation='h', font=dict(color='#FFFFFF')),
        margin=dict(l=10, r=10, t=100, b=50)
    )

fig = go.Figure(go.Scatter(x=Date, y=New_cases, mode='lines'))
updateFigure(fig)

fig.show()

enter image description here

Karisakarissa answered 14/6, 2023 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.