
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Working With Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - LaTeX
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Celluloid Library
- Matplotlib - Blitting
- Matplotlib - Toolkits
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Widgets
- Matplotlib - Cursor Widget
- Matplotlib - Annotated Cursor
- Matplotlib - Buttons Widget
- Matplotlib - Check Buttons
- Matplotlib - Lasso Selector
- Matplotlib - Menu Widget
- Matplotlib - Mouse Cursor
- Matplotlib - Multicursor
- Matplotlib - Polygon Selector
- Matplotlib - Radio Buttons
- Matplotlib - RangeSlider
- Matplotlib - Rectangle Selector
- Matplotlib - Ellipse Selector
- Matplotlib - Slider Widget
- Matplotlib - Span Selector
- Matplotlib - Textbox
- Matplotlib Plotting
- Matplotlib - Line Plots
- Matplotlib - Area Plots
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Arrow Demo
- Matplotlib - Fancy Boxes
- Matplotlib - Zorder Demo
- Matplotlib - Hatch Demo
- Matplotlib - Mmh Donuts
- Matplotlib - Ellipse Demo
- Matplotlib - Bezier Curve
- Matplotlib - Bubble Plots
- Matplotlib - Stacked Plots
- Matplotlib - Table Charts
- Matplotlib - Polar Charts
- Matplotlib - Hexagonal bin Plots
- Matplotlib - Violin Plot
- Matplotlib - Event Plot
- Matplotlib - Heatmap
- Matplotlib - Stairs Plots
- Matplotlib - Errorbar
- Matplotlib - Hinton Diagram
- Matplotlib - Contour Plot
- Matplotlib - Wireframe Plots
- Matplotlib - Surface Plots
- Matplotlib - Triangulations
- Matplotlib - Stream plot
- Matplotlib - Ishikawa Diagram
- Matplotlib - 3D Plotting
- Matplotlib - 3D Lines
- Matplotlib - 3D Scatter Plots
- Matplotlib - 3D Contour Plot
- Matplotlib - 3D Bar Plots
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - 3D Vignettes
- Matplotlib - 3D Volumes
- Matplotlib - 3D Voxels
- Matplotlib - Time Plots and Signals
- Matplotlib - Filled Plots
- Matplotlib - Step Plots
- Matplotlib - XKCD Style
- Matplotlib - Quiver Plot
- Matplotlib - Stem Plots
- Matplotlib - Visualizing Vectors
- Matplotlib - Audio Visualization
- Matplotlib - Audio Processing
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Cheatsheet
- Matplotlib - Useful Resources
- Matplotlib - Discussion
Matplotlib - Ticks and Tick Labels
In the context of graphs and plotting, ticks are the small lines that show the scale of x and y-axes, providing a clear representation of the value associated with each tick. Whereas tick labels are the textual or numeric annotations associated with each tick on an axis, providing a clear representation of the value associated with each tick.
The following image represents the ticks and tick labels on a graph −

There are two types of ticks in a visualization: major and minor. Major tick marks are positions where labels can be placed, providing significant reference points. On the other hand, minor tick marks indicate values between the major ticks. See the below image for reference −

Ticks and Tick labels in Matplotlib
Matplotlib provides several tools for controlling ticks and tick labels on the axes, allowing you to customize ticks and tick labels by specifying custom positions and labels.
While the default behavior of Matplotlib is, automatically determines the number of ticks and their positions based on the range and scale of the data being plotted, this flexibility is useful for providing additional information or formatting the labels according to the preferences.
Basic Plot with Default Ticks and Lables
By default Matplotlib determines suitable tick positions and labels based on the provided data.
Example
This example generates a simple sine wave plot with default ticks and lables.
import matplotlib.pyplot as plt import numpy as np # Sampel data t = np.linspace(0.0, 2.0, 201) s = np.sin(2 * np.pi * t) # Create plot fig, ax = plt.subplots(figsize=(7,4)) # Plot the data plt.plot(t, s) plt.title('Sine Wave') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
Output
On executing the above program you will get the following example −

Customizing Ticks and Tick Labels
Customizing ticks and tick labels can be achieved by using functions such as axes.set_xticks() and axes.set_yticks(), as well as plt.xticks() and plt.yticks().
Basic customization of ticks and tick labels may involve labeling ticks with customized strings, rotating custom tick labels, and few more.
Example
The following example demonstrates how to customize the x-axis ticks with specific positions, labels, and rotation. It converts or scales the axis values and redefine the tick frequency.
import matplotlib.pyplot as plt import numpy as np # Create plot fig, ax = plt.subplots(figsize=(7,4)) # Sample data x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) # Plotting the data plt.plot(x, y) plt.title('Sine Wave') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Custom ticks and tick labels custom_ticks = [0, np.pi/2, np.pi, (3*np.pi)/2, 2*np.pi] custom_tick_labels = ['0', '$\pi/2$', '$\pi$', '3$\pi/2$', '2$\pi$'] ax.set_xticks(custom_ticks, custom_tick_labels, rotation='vertical') # Display the plot plt.show()
Output
On executing the above program you will get the following example −

Example
This example demonstrates how to customize tick label positions and visibility, adjust separation between tick labels and axis labels, and turn off ticks and marks on a Matplotlib plot axis.
import numpy as np import matplotlib.pyplot as plt # Create subplots fig, ax = plt.subplots(1, 1) # Plot some data plt.plot([1, 2, 3, 4, 5]) # Set tick positions and labels plt.xticks([1, 2, 3, 4, 5]) # show the tick labels on top of the plot? ax.xaxis.set_tick_params(labeltop=True) # set gap between the tick labels and axis labels plt.xlabel("X-axis", labelpad=25) # turn off the ticks and marks on botton x axis ax.tick_params(axis='both', which='both', bottom=False, top=True, left=True, right=False, labelbottom=True, labelleft=True) # Displaying the plot plt.show()
Output
On executing the above code you will get the following output −

Adding Minor Ticks
Minor ticks are initially disabled but can be enabled without labels by configuring the minor locator. The ticker module within the Matplotlib library provides two classes, namely MultipleLocator and AutoMinorLocator. These classes facilitate the placement of ticks at multiples of a specified base and allow you to specify a fixed number of minor intervals per major interval, respectively. As an alternative, the minorticks_on() method can be used to enable them.
Example
The following example demonstrates how to add the minor ticks to a plot. Here we will use the minorticks_on() method.
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) fig, ax = plt.subplots(figsize=(7,4)) # Plotting the data plt.plot(x, y) plt.title('Sine Wave') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Adding minor ticks plt.minorticks_on() # Display the plot plt.show()
Output
On executing the above program you will get the following example −
