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 change the font size of scientific notation in Matplotlib?
Scientific notation in Matplotlib can sometimes appear too small or too large for your plot. You can control the font size of scientific notation using various methods including rcParams, tick_params(), and direct axis formatting.
Basic Scientific Notation with Default Font Size
First, let's create a plot with scientific notation using default settings ?
import matplotlib.pyplot as plt
# Sample data with large values
x = [10000, 20000, 300000, 34000, 50000, 100000]
y = [1, 2, 3, 4, 5, 6]
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='red', marker='o')
plt.ticklabel_format(axis="x", style="sci", scilimits=(0, 0))
plt.title("Default Scientific Notation")
plt.show()
Method 1: Using rcParams
You can globally change the font size of scientific notation using rcParams ?
import matplotlib.pyplot as plt
# Set font size for scientific notation
plt.rcParams["font.size"] = 14
plt.rcParams["axes.labelsize"] = 12
x = [10000, 20000, 300000, 34000, 50000, 100000]
y = [1, 2, 3, 4, 5, 6]
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='blue', marker='s')
plt.ticklabel_format(axis="x", style="sci", scilimits=(0, 0))
plt.title("Larger Font Size with rcParams")
plt.show()
Method 2: Using tick_params()
For more control, use tick_params() to specifically target tick labels ?
import matplotlib.pyplot as plt
x = [10000, 20000, 300000, 34000, 50000, 100000]
y = [1, 2, 3, 4, 5, 6]
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='green', marker='^')
plt.ticklabel_format(axis="x", style="sci", scilimits=(0, 0))
# Set font size for tick labels
plt.tick_params(axis='x', labelsize=16)
plt.tick_params(axis='y', labelsize=12)
plt.title("Custom Font Size with tick_params()")
plt.show()
Method 3: Direct Axis Formatting
You can also format specific axes using axis objects ?
import matplotlib.pyplot as plt
x = [10000, 20000, 300000, 34000, 50000, 100000]
y = [1, 2, 3, 4, 5, 6]
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(x, y, color='purple', marker='d')
ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0))
# Format specific axis
ax.tick_params(axis='x', labelsize=18)
ax.set_title("Direct Axis Font Size Control", fontsize=14)
plt.show()
Comparison of Methods
| Method | Scope | Best For |
|---|---|---|
rcParams |
Global | Consistent styling across plots |
tick_params() |
Current plot | Quick adjustments |
| Axis objects | Specific axis | Fine-grained control |
Conclusion
Use rcParams for global font size changes, tick_params() for quick plot-specific adjustments, and axis objects for precise control over individual axes. The ticklabel_format() method enables scientific notation display.
