Re-Name the axes in plotly express

How i can rename the title for the x and y-axis?

import plotly.express as px
import pandas as pd
import numpy as np
import os

df = pd.read_csv(‘C:\test.csv’)
df_long=pd.melt(df , id_vars=[‘ID’])
fig = px.line(df_long, x=‘ID’, y=‘value’, color=‘variable’)
fig.show()

Hello!

You can use the update_layout() method to change your figure’s xaxis_title and yaxis_title attributes, like this:

import plotly.express as px

df = px.data.gapminder().query("country=='Canada'")

fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')

fig.update_layout(
    xaxis_title="x Axis Title",
    yaxis_title="y Axis Title",
)

fig.show()

I hope that answers your question!

1 Like

I would recommend using the labels keyword argument instead, as this also changes the hover text, and works no matter what you map the values to (i.e. if you want set color to value instead as you iterate through your explorations): https://plotly.com/python/styling-plotly-express/

import plotly.express as px
import pandas as pd
import numpy as np
import os

df = pd.read_csv(‘C:\test.csv’)
df_long=pd.melt(df , id_vars=[‘ID’])
fig = px.line(df_long, x=‘ID’, y=‘value’, color=‘variable’, labels={"value": "new label", "ID": "new label"})
fig.show()
1 Like

It is working well now. Thank you!