- 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
How to add third level of ticks in Python Matplotlib?
To add third level of ticks in Python Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create t and s data points using numpy.
- Create a figure and a set of subplots.
- Plot t and s using plot() method.
- Create a twin Axes sharing the Y-axis.
- Plot t and s using plot() method, on axis one.
- Set X-axis tick position.
- Create majors, minors and third level of ticks value (thirds).
- Set major and minor ticks locator with majors, minors and third tick values (thirds)
- Set ticks length using tick_params().
- Plot a horizontal line with gray color.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True t = np.arange(0.0, 100.0, 0.1) s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01) fig, ax = plt.subplots() plt.plot(t, s) ax1 = ax.twiny() ax1.plot(t, s) ax1.xaxis.set_ticks_position('bottom') majors = np.linspace(0, 100, 6) minors = np.linspace(0, 100, 11) thirds = np.linspace(0, 100, 101) ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(majors)) ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minors)) ax1.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([])) ax1.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(thirds)) ax1.tick_params(which='minor', length=2) ax.tick_params(which='minor', length=4) ax.tick_params(which='major', length=6) ax.grid(which='both', axis='x', linestyle='--') plt.axhline(color='gray') plt.show()
Output
- Related Articles
- How to add Matplotlib Colorbar Ticks?
- How to set axis ticks in multiples of pi in Python Matplotlib?
- How to customize X-axis ticks in Matplotlib?
- How to set the number of ticks in plt.colorbar in Matplotlib?
- How to change the spacing between ticks in Matplotlib?
- How to disable the minor ticks of a log-plot in Matplotlib?
- How to change the color of the ticks in the colorbar in Matplotlib?
- How to set the ticks on a Fixed Position in Matplotlib?
- How to create minor ticks for a polar plot in matplotlib?
- How to hide ticks label in Python but keep the ticks in place?
- How can I set the location of minor ticks in Matplotlib?
- How to move labels from bottom to top without adding "ticks" in Matplotlib?
- Adjusting gridlines and ticks in Matplotlib imshow
- Adding extra axis ticks using Matplotlib
- How can I change the font size of ticks of axes object in Matplotlib?

Advertisements