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 make Matplotlib show all X coordinates?
To show all X coordinates (or Y coordinates) in a Matplotlib plot, we can use the xticks() and yticks() methods to explicitly specify which tick marks should appear on the axes.
Basic Approach
The key is to pass your data array directly to xticks() and yticks() methods. This forces Matplotlib to display tick marks for every data point instead of using its default tick spacing.
Example
Here's how to display all X and Y coordinates on a simple line plot ?
import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.arange(0, 10, 1) y = np.arange(0, 10, 1) # Remove margins to show full range plt.margins(x=0, y=0) # Plot the data plt.plot(x, y) # Show all X and Y coordinates plt.xticks(x) plt.yticks(y) plt.show()
Advanced Example with Custom Data
Here's an example with non-sequential data points ?
import numpy as np
import matplotlib.pyplot as plt
# Create custom data
x = [1, 3, 5, 7, 12, 15]
y = [2, 8, 6, 10, 4, 9]
plt.figure(figsize=(8, 5))
# Plot the data
plt.plot(x, y, marker='o')
# Show all X coordinates
plt.xticks(x)
# Show specific Y coordinates (optional)
plt.yticks(range(0, max(y) + 2, 2))
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Plot Showing All X Coordinates')
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
The xticks() and yticks() methods accept several useful parameters ?
- ticks − Array of tick positions
- labels − Custom labels for each tick (optional)
- rotation − Rotate tick labels to prevent overlap
- fontsize − Control text size
import matplotlib.pyplot as plt
# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
values = [10, 25, 30, 45, 20]
x_positions = range(len(months))
plt.figure(figsize=(8, 5))
plt.plot(x_positions, values, marker='s')
# Show all X positions with custom labels
plt.xticks(x_positions, months, rotation=45, fontsize=10)
plt.ylabel('Values')
plt.title('Monthly Data with All X Labels')
plt.tight_layout()
plt.show()
Comparison
| Method | Use Case | Best For |
|---|---|---|
plt.xticks(x) |
Show all data points | Small datasets |
plt.xticks(range(min, max, step)) |
Regular intervals | Large datasets |
plt.xticks(positions, labels) |
Custom labels | Categorical data |
Conclusion
Use plt.xticks(x) to show all X coordinates by passing your data array directly. For large datasets, consider showing only key points to avoid overcrowded labels. Add rotation and adjust font size for better readability.
