- 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
Change grid interval and specify tick labels in Matplotlib
Using plt.figure() method, we can create a figure and thereafter, we can create an axis. Using set_xticks and set_yticks, we can change the ticks format and ax.grid could help to specify the grid interval.
Steps
Create a new figure, or activate an existing figure, using fig = plt.figure() method.
Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrow = 1, ncols = 1 and index = 1.
Get or set the current tick locations and labels of the X-axis.
Get or set the current tick locations and labels of the X-axis. With minor = True, Grid.
Get or set the current tick locations and labels of the Y-axis.
Get or set the current tick locations and labels of the Y-axis. With minor = True, Grid.
Lay out a grid in current line style, using grid() method.
To show the figure we can use the plt.show() method.
Example
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) major_tick = [10, 20, 30, 40, 50] minor_tick = [5, 15, 25, 35, 45] ax.set_xticks(major_tick) # Grid ax.set_xticks(minor_tick, minor=True) ax.set_yticks(major_tick) # Grid ax.set_yticks(minor_tick, minor=True) ax.grid(which='both') ax.grid(which='minor', alpha=1) ax.grid(which='major', alpha=2) plt.show()
Output
- Related Articles
- How to change the separation between tick labels and axis labels in Matplotlib?
- Hiding major tick labels while showing minor tick labels in Matplotlib
- Centering x-tick labels between tick marks in Matplotlib
- Show tick labels when sharing an axis in Matplotlib
- How to rotate tick labels in a subplot in Matplotlib?
- Hide axis values but keep axis tick labels in matplotlib
- Getting empty tick labels before showing a plot in Matplotlib
- How to decrease the density of tick labels in subplots in Matplotlib?
- How to increase/reduce the fontsize of X and Y tick labels in Matplotlib?
- How to show tick labels on top of a matplotlib plot?
- How to show minor tick labels on a log-scale with Matplotlib?
- What is the correct way to replace matplotlib tick labels with computed values?
- How to change the datetime tick label frequency for Matplotlib plots?
- Rotate tick labels for Seaborn barplot in Matplotib
- How to remove a frame without removing the axes tick labels from a Matplotlib figure in Python?
