- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Adjusting gridlines and ticks in Matplotlib imshow
To adjust gridlines and ticks in matplotlib imshow(), we can take the following steps−
- Create data, a 2D array, using numpy.
- Using imshow() method, display data as an image.
- Set xticks and yticks using set_xticks and set_yticks method.
- To set the xticklabels and yticklabels, use set_xticklabels and set_yticklabels method.
- Lay out a grid in current line style. Supply the list of x an y positions using grid() method.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(9, 9) plt.imshow(data, interpolation="nearest") ax = plt.gca() ax.set_xticks(np.arange(-.5, 9, 1)) ax.set_yticks(np.arange(-.5, 9, 1)) ax.set_xticklabels(np.arange(0, 10, 1)) ax.set_yticklabels(np.arange(0, 10, 1)) ax.grid(color='red', linestyle='-.', linewidth=1) plt.show()
Output
- Related Articles
- Adjusting Text background transparency in Matplotlib
- How to hide axes and gridlines in Matplotlib?
- Plotting an imshow() image in 3d in Matplotlib
- Adjusting the heights of individual subplots in Matplotlib in Python
- What is the difference between plt.show and cv2.imshow in Matplotlib?
- Remove white border when using subplot and imshow in Python Matplotlib
- How to add legend to imshow() in Matplotlib?
- Defining a discrete colormap for imshow in Matplotlib
- Matplotlib – How to set xticks and yticks with imshow plot?
- Adding extra axis ticks using Matplotlib
- How to add Matplotlib Colorbar Ticks?
- Change values on matplotlib imshow() graph axis
- Add minor gridlines to Matplotlib plot using Seaborn
- Getting vertical gridlines to appear in line plot in matplotlib
- How to customize X-axis ticks in Matplotlib?

Advertisements