Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to repress scientific notation in factorplot Y-axis in Seaborn / Matplotlib?
When working with large numbers in Seaborn plots, the Y-axis often displays values in scientific notation (e.g., 1e6). To suppress this and show plain numbers, we can use the ticklabel_format() method with style="plain".
Understanding the Problem
Seaborn's catplot() (formerly factorplot()) uses Matplotlib's automatic formatting, which switches to scientific notation for large numbers. This can make plots harder to read in many contexts.
Solution Using ticklabel_format()
The plt.ticklabel_format() method controls how tick labels are displayed. Setting style="plain" forces regular decimal notation instead of scientific notation.
Example
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df = pd.DataFrame({"category": ["A", "B", "C", "D", "E"],
"values": [10000, 50000, 70000, 90000, 10000]})
# Create catplot
sns.catplot(y="values", x="category", kind='bar', data=df, height=3.5)
# Suppress scientific notation
plt.ticklabel_format(style='plain', axis='y')
plt.show()
Alternative Methods
Using gca() for More Control
You can also access the current axes directly for more precise control ?
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame({"category": ["A", "B", "C"],
"values": [150000, 250000, 350000]})
fig = sns.catplot(y="values", x="category", kind='bar', data=df, height=4)
# Access the axes and format
ax = plt.gca()
ax.ticklabel_format(style='plain', axis='y')
plt.show()
Using FuncFormatter for Custom Formatting
For more advanced formatting, you can use FuncFormatter ?
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from matplotlib.ticker import FuncFormatter
df = pd.DataFrame({"category": ["X", "Y", "Z"],
"values": [1200000, 1500000, 1800000]})
sns.catplot(y="values", x="category", kind='bar', data=df, height=4)
# Custom formatter function
def format_func(value, tick_number):
return f'{int(value):,}'
plt.gca().yaxis.set_major_formatter(FuncFormatter(format_func))
plt.show()
Comparison of Methods
| Method | Use Case | Customization |
|---|---|---|
ticklabel_format(style='plain') |
Simple suppression of scientific notation | Limited |
gca().ticklabel_format() |
More control over specific axes | Medium |
FuncFormatter |
Custom formatting (commas, currency, etc.) | Full |
Conclusion
Use plt.ticklabel_format(style='plain', axis='y') to suppress scientific notation in Seaborn plots. For advanced formatting needs, consider using FuncFormatter with custom formatting functions.
