4

I'm using the px.bar() function in Plotly Express to create some simple bar charts.

My code is as follows:

import plotly.express as px
import pandas as pd

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                       })

df1 = pd.DataFrame(test_df.groupby(by=['Manufacturer', 'Sector'])['Value'].sum())

df1.reset_index(inplace=True)

fig = px.bar(df1, x='Manufacturer', y='Value', color='Sector', barmode='group', text='Value')

fig.show()

Is it possible to add comma separators for the thousands values on the bars? For example, "70,900" instead of "70900"?

I checked the docs at https://plotly.com/python-api-reference/generated/plotly.express.bar.html#plotly.express.bar, but didn't see anything that would govern the behavior I'm looking for.

Thanks in advance!

equanimity
  • 2,371
  • 3
  • 29
  • 53

3 Answers3

6

There is a formater with update_traces function, texttemplate keyword, following the D-3 format.

The syntax would be:

fig.update_traces(texttemplate='%{text:,}')
Woden
  • 1,054
  • 2
  • 13
  • 26
3

You need to append the commas to the dataframe before plotting. So, you can do this:

...
...
df1["Value"] = df1["Value"].apply(lambda x : "{:,}".format(x))
fig = px.bar(df1, x='Manufacturer', y='Value', color='Sector', barmode='group', text='Value')
fig.show()

Which results in the following graph: enter image description here

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
  • Thank you, @Anwarvic. This solution works on a subset of the data. But when expanded to a larger data set, I get the following error: `ValueError: Unable to parse string "90.,0"`. The value in the source data is just an integer: "90". – equanimity Jun 13 '20 at 18:18
  • I edited my answer, could you try it one more time? – Anwarvic Jun 13 '20 at 18:30
  • Your updated solutions works, @Anwarvic when I update the data set in this "toy" example in this question. However, on the actual data set, your solution now throws: `Traceback (most recent call last): File "pandas\_libs\lib.pyx", line 1963, in pandas._libs.lib.maybe_convert_numeric ValueError: Unable to parse string "100,0.0" at position 0`. The actual data is contained in an Excel xlsx worksheet (not sure if that matters). Wondering if there is a native approach that can be set in the px.bar() function, as opposed to transforming the data frame? – equanimity Jun 13 '20 at 23:00
0

This worked for me:

 fig.update_layout(yaxis_tickformat = ',')
Fares Sayah
  • 121
  • 1
  • 5