0

I currently have this graph, created using:

fg = sns.catplot(x='metric', y='number', hue='drop', data=df, kind='bar')

enter image description here

I need to change the axis labels to have commas.

I tried:

df['number'] = df['number'].apply(lambda x: "{:,}".format(x))

However this errors, because neither the x or y values are numeric then (the above changes the data type from numeric to object).

How can I get commas in the y axis values?

I cannot use get_yaxis().set_major_formatter because this is not an attribute of facetgrid.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Darcey BM
  • 301
  • 1
  • 5
  • 20
  • 1
    FYI, you can get the axes object using `fg.axes` as shown [here](https://stackoverflow.com/a/25213438/5851928) – DavidG Aug 07 '20 at 10:08

1 Answers1

1

The for loop in the following code creates the comma format using StrMethodFormatter.

import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import matplotlib.ticker as ticker
import pandas as pd
import seaborn as sns

#Theme
sns.set(style="darkgrid")

#Example data frame
df = pd.DataFrame()
df["Value"]    = [1000000, 2000000, 3000000, 2000000, 5000000] 
df["Category"] = ["A","B","C","D","E"]
fg = sns.catplot(x='Category', y='Value', data=df, kind='bar')

for ax in fg.axes.flat:
    ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))

#So that axis show
plt.tight_layout()

#Show plot
plt.show()

enter image description here

Rodrigo Zepeda
  • 1,935
  • 2
  • 15
  • 25