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
Selected Reading
How to disable the minor ticks of a log-plot in Matplotlib?
When creating logarithmic plots in Matplotlib, minor ticks are automatically displayed between major ticks. You can disable these minor ticks using the minorticks_off() method to create cleaner visualizations.
Basic Syntax
plt.minorticks_off() # Disable minor ticks for current axes ax.minorticks_off() # Disable minor ticks for specific axes
Example: Comparing With and Without Minor Ticks
Let's create two subplots to compare log plots with and without minor ticks ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [10, 4]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = np.logspace(0, 3, 20) # Logarithmic spacing from 1 to 1000
y = x ** 0.5
# Subplot 1: With minor ticks (default)
plt.subplot(121)
plt.plot(x, y, 'ro-', linewidth=2, markersize=4)
plt.xscale('log')
plt.yscale('log')
plt.grid(True, alpha=0.3)
plt.title("With Minor Ticks")
plt.xlabel("X values (log scale)")
plt.ylabel("Y values (log scale)")
# Subplot 2: Without minor ticks
plt.subplot(122)
plt.plot(x, y, 'go-', linewidth=2, markersize=4)
plt.xscale('log')
plt.yscale('log')
plt.minorticks_off() # Disable minor ticks
plt.grid(True, alpha=0.3)
plt.title("Without Minor Ticks")
plt.xlabel("X values (log scale)")
plt.ylabel("Y values (log scale)")
plt.tight_layout()
plt.show()
Using Axes Object Method
You can also control minor ticks for specific axes using the object-oriented approach ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.logspace(1, 4, 30)
y = np.log10(x) * 2
# Create figure and axes
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
# Left plot: Default with minor ticks
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_xscale('log')
ax1.set_title('Default Log Plot')
ax1.grid(True, alpha=0.3)
ax1.set_xlabel('X (log scale)')
ax1.set_ylabel('Y values')
# Right plot: Minor ticks disabled
ax2.plot(x, y, 'm-', linewidth=2)
ax2.set_xscale('log')
ax2.minorticks_off() # Disable minor ticks for this specific axes
ax2.set_title('Minor Ticks Disabled')
ax2.grid(True, alpha=0.3)
ax2.set_xlabel('X (log scale)')
ax2.set_ylabel('Y values')
plt.tight_layout()
plt.show()
Controlling Minor Ticks on Specific Axes
You can disable minor ticks on only the x-axis or y-axis using tick locators ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullLocator
# Create exponential data
x = np.linspace(1, 100, 50)
y = np.exp(x/20)
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'r-', linewidth=2)
plt.xscale('log')
plt.yscale('log')
# Disable minor ticks only on x-axis
plt.gca().xaxis.set_minor_locator(NullLocator())
plt.title('Minor Ticks Disabled Only on X-axis')
plt.xlabel('X values (log scale)')
plt.ylabel('Y values (log scale)')
plt.grid(True, alpha=0.3)
plt.show()
When to Disable Minor Ticks
Consider disabling minor ticks when:
- Clarity ? Too many ticks make the plot cluttered
- Presentation ? Clean plots for reports or publications
- Limited space ? Small subplots with tight layouts
- Focus ? Emphasize major tick values only
Conclusion
Use minorticks_off() to disable minor ticks in logarithmic plots for cleaner visualization. This method works with both pyplot and axes objects, giving you flexibility in controlling tick appearance.
Advertisements
