Skip to content
Advertisement

plotly axis labels don’t show

I am trying to plot something with plotly bt the axis labels don’t show. I can’t find what I’m doing wrong.

import plotly.graph_objects as go
df=pd.DataFrame({'month':[0,1,2,3],'counts1':[14,2,4,5], 'counts2':[19,3,4,1], 'counts3':[11,1,6,9]})

cols = df.drop(columns={'month'}).columns
fig = go.Figure()
for col in cols:
    fig.add_trace(go.Scatter(x=df['month'], y=df[col], mode='lines',
        name=col
    ))
fig.update_layout(scene = dict(
                    xaxis_title='X AXIS TITLE',
                    yaxis_title='Y AXIS TITLE'),
                    width=1000
                    )

fig.show()

Advertisement

Answer

You just need to set xaxis_title and yaxis_title

import plotly.graph_objects as go

df = pd.DataFrame(
    {
        "month": [0, 1, 2, 3],
        "counts1": [14, 2, 4, 5],
        "counts2": [19, 3, 4, 1],
        "counts3": [11, 1, 6, 9],
    }
)

cols = df.drop(columns={"month"}).columns
fig = go.Figure()
for col in cols:
    fig.add_trace(go.Scatter(x=df["month"], y=df[col], mode="lines", name=col))
fig.update_layout(xaxis_title="X AXIS TITLE", yaxis_title="Y AXIS TITLE", width=1000)


fig.show()

Using Plotly Express

px.line(df, x="month", y=[c for c in df.columns if c != "month"]).update_layout(
    xaxis_title="X AXIS TITLE", yaxis_title="Y AXIS TITLE"
)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement