Search code examples
pythonplotly-express

How to change currency format in plotly express hover data?


I have been trying to change the currency format in this plotly express figure in the hover data, without any success. I am looking to change it from US dollars to my home currency, GBP. Does anyone know what i need to do to achieve this? The documentation doesn't really go into it much.

fig = px.bar(
df, 
x=df['cat_level2'], 
y=df['count'], 
labels={"cat_level2": "Category", "count": "Product Count", "max":"Maximum Price", "mean":"Average Price", "min":"Minimum Price"}, 
hover_name="cat_level2", 
hover_data={"cat_level2":False, "max":":$.2f", "mean":":$.2f", "min":":$.2f"}
)

fig.show()

If I change the $ symbols to pound symbols (£) this shows customdata in the chart tooltip. According to them they are using the d3 library for currency formatting which claims to use the system locale. I have checked that i am using the correct locale, (which is en_GB.utf-8), so i don't know what else to check?


Solution

  • Added pound signs as hover data for population based on examples from the official website.

    import plotly.express as px
    
    df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
    fig = px.bar(df, y='pop', x='country') 
    fig.update_traces(hovertemplate='pop: \u00A3%{y}')
    
    fig.show()
    

    enter image description here