- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Overlapping Y-axis tick label and X-axis tick label in Matplotlib
To reduce the chances of overlapping between x and y tick labels in matplotlib, we can take the following steps −
Create x and y data points using numpy.
Add a subplot to the current figure at index 1 (nrows=1 and ncols=2).
Set x and y margins to 0.
Plot x and y data points and add a title to this subplot, i.e., "Overlapping".
Add a subplot to the current figure at index 2 (nrows=1 and ncols=2).
Set x and y margins to 0.
Plot x and y data points and add a title to this subplot, i.e., "Non Overlapping".
The objective of MaxNLocator and prune ="lower" is that the smallest tick will be removed.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xs = np.linspace(0, 5, 10) ys = np.linspace(0, 5, 10) plt.subplot(121) plt.margins(x=0, y=0) plt.plot(xs, ys) plt.title("Overlapping") plt.subplot(122) plt.margins(x=0, y=0) plt.plot(xs, ys) plt.title("Non overlapping") plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower')) plt.gca().yaxis.set_major_locator(MaxNLocator(prune='lower')) plt.show()
Output
- Related Articles
- Changing the color of a single X-axis tick label in Matplotlib
- Matplotlib log scale tick label number formatting
- How can I move a tick label without moving corresponding tick in Matplotlib?
- Hide axis values but keep axis tick labels in matplotlib
- How to adjust 'tick frequency' in Matplotlib for string Y-axis?
- Show tick labels when sharing an axis in Matplotlib
- How to append a single labeled tick to X-axis using matplotlib?
- How to access axis label object in Matplotlib?
- How to change the datetime tick label frequency for Matplotlib plots?
- How to adjust 'tick frequency' in Matplotlib for string X-axis?
- Turn off the left/bottom axis tick marks in Matplotlib
- How to print the Y-axis label horizontally in a Matplotlib/Pylab chart?
- Centering x-tick labels between tick marks in Matplotlib
- How to change the separation between tick labels and axis labels in Matplotlib?
- How to remove the first and last ticks label of each Y-axis subplot in Matplotlib?

Advertisements