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:
JavaScript
x
5
1
Date = df[df.Country=="India"].Date
2
New_cases = df[df.Country=="India"]['7day_rolling_avg']
3
4
px.line(df,x=Date, y=New_cases, title="India Daily New Covid Cases")
5
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”
Advertisement
Answer
- simple case of setting axis title
JavaScript
1
4
1
update_layout(
2
xaxis_title="Date", yaxis_title="7 day avg"
3
)
4
full code as MWE
JavaScript
1
21
21
1
import pandas as pd
2
import io, requests
3
4
df = pd.read_csv(
5
io.StringIO(
6
requests.get(
7
"https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv"
8
).text
9
)
10
)
11
df["Date"] = pd.to_datetime(df["date"])
12
df["Country"] = df["location"]
13
df["7day_rolling_avg"] = df["daily_people_vaccinated_per_hundred"]
14
15
Date = df[df.Country == "India"].Date
16
New_cases = df[df.Country == "India"]["7day_rolling_avg"]
17
18
px.line(df, x=Date, y=New_cases, title="India Daily New Covid Cases").update_layout(
19
xaxis_title="Date", yaxis_title="7 day avg"
20
)
21