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 set the value of the axis multiplier in matplotlib?
To set the value of the axis multiplier in matplotlib, you can control the spacing between major ticks on your plot axes. This is useful for creating custom tick intervals that make your data more readable.
What is an Axis Multiplier?
An axis multiplier determines the interval between major ticks on an axis. For example, a multiplier of 6 means ticks will appear at 6, 12, 18, 24, etc.
Steps to Set Axis Multiplier
Set the figure size and adjust the padding between and around the subplots.
Create x data points using numpy.
Plot x and x2 using
plot()method.Get the current axis of the figure.
Initialize a variable
multiplier, i.e., a value of the axis multiplier.Set a tick on each integer multiple of a base within the view interval.
Set the locator of the major ticker.
To display the figure, use
show()method.
Example
import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create x data points x = np.linspace(1, 100, 100) # Plot x and x^2 plt.plot(x, x**2) # Get the current axis ax = plt.gca() # Axis multiplier multiplier = 6 # Set a tick on each integer multiple locator = plt.MultipleLocator(multiplier) # Set the locator of the major ticker ax.xaxis.set_major_locator(locator) plt.show()
Setting Y-Axis Multiplier
You can also set the multiplier for the y-axis ?
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(0, 20, 100)
y = x**2
plt.plot(x, y)
# Get current axes
ax = plt.gca()
# Set multiplier for both axes
x_multiplier = 5
y_multiplier = 100
# Apply locators
ax.xaxis.set_major_locator(plt.MultipleLocator(x_multiplier))
ax.yaxis.set_major_locator(plt.MultipleLocator(y_multiplier))
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Custom Axis Multipliers')
plt.show()
Different Multiplier Values
Here's how different multiplier values affect the tick spacing ?
import matplotlib.pyplot as plt
import numpy as np
# Create subplots for comparison
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 4))
x = np.linspace(0, 30, 100)
y = x**2
# Plot 1: Multiplier = 2
ax1.plot(x, y)
ax1.xaxis.set_major_locator(plt.MultipleLocator(2))
ax1.set_title('Multiplier = 2')
# Plot 2: Multiplier = 5
ax2.plot(x, y)
ax2.xaxis.set_major_locator(plt.MultipleLocator(5))
ax2.set_title('Multiplier = 5')
# Plot 3: Multiplier = 10
ax3.plot(x, y)
ax3.xaxis.set_major_locator(plt.MultipleLocator(10))
ax3.set_title('Multiplier = 10')
plt.tight_layout()
plt.show()
Conclusion
Use plt.MultipleLocator() with set_major_locator() to control axis tick intervals. Choose multiplier values that make your data readable and avoid overcrowded or sparse tick marks.
