Search code examples
pythonplotlyyaxis

Python Plotly Facet Plot Y-axis title change


when i ran the following:

df = px.data.tips()
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day",
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
fig.update_layout(yaxis_title="Total Bill ($)")
fig.show()

The problem I encountered is that out of the 2 y-axis titles for my plot, only one of them changed to 'Total Bill ($)' and the other one stays at 'total_bill'.

(https://i.sstatic.net/oUu0J.png)

Anyone knows whats going on?


Solution

  • A non-plotly option is to rename that column:

    fig = px.bar(df.rename(columns={"total_bill": "Total Bill ($)"}), 
                 x="sex", y="Total Bill ($)", color="smoker", barmode="group", 
                 facet_row="time", facet_col="day",
                 category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], 
                                  "time": ["Lunch", "Dinner"]})
    fig.show()
    

    Output: enter image description here