Open In App

Matplotlib – Setting Ticks and Tick Labels

Last Updated : 26 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss how set Ticks and Tick labels in a graph.

Ticks are the markers denoting data points on the axes and tick labels are the name given to ticks. By default matplotlib itself marks the data points on the axes but it has also provided us with setting their own axes having ticks and tick labels of their choice.

Methods used:

  • plt.axes(*args, emit=True, **kwargs): For setting the axes for our plot with parameter rect as [left,bottom,width,height] for  setting axes position.
  • plt.axes().set_xticks() and plt.axes().set_yticks() : For setting ticks on x-axis and y-axis respectively. having data in form of a list set as parameter.
  • plt.axes().set_xlabels() and plt.axes().set_ylabels() : To set labels of our ticks with parameters in form of list.

Below are some examples which depict how to add ticks and ticklabels in a plot:

Example 1: 

Python3




# import required module
import matplotlib.pyplot as plt
  
# assign coordinates
x = y = [i for i in range(0, 10)]
ax = plt.axes()
  
# depict illustration
plt.plot(x, y, color="lime")
  
# setting ticks for x-axis
ax.set_xticks([2, 4, 6, 8, 10])
  
# setting ticks for y-axis
ax.set_yticks([1, 3, 5, 7, 9])
  
plt.show()


Output:

Example 2:

Python3




# import required module
import matplotlib.pyplot as plt
  
# assign coordinates
x = y = [i for i in range(0, 10)]
ax = plt.axes()
  
# depict illustration
plt.plot(x, y, color="lime")
  
# setting ticks for x-axis
ax.set_xticks([2, 4, 6, 8, 10])
  
# setting label for x tick
ax.set_xticklabels(['Geeks', 'for', 'geeks', '!'])
  
# setting ticks for y-axis
ax.set_yticks([1, 3, 5, 7, 9])
  
# setting label for y tick
ax.set_yticklabels(['A', 'B', 'C', 'D'])
  
plt.show()


Output:

Example 3:

Python3




# import required modules
import matplotlib.pyplot as plt
import numpy as np
import math
  
# assign coordinates
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
ax = plt.axes()
  
# depict illustration
plt.plot(x, y, color="lime")
  
# setting ticks for x-axis
ax.set_xticks([0, 2, 4, 6])
  
# setting ticks for y-axis
ax.set_yticks([-1, 0, 1])
  
plt.show()


Output:

Example 4:

Python3




# import required modules
import matplotlib.pyplot as plt
import numpy as np
import math
  
# assign coordinates
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
ax = plt.axes()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
  
# depict illustration
plt.plot(x, y, color="lime")
  
# setting ticks for x-axis
ax.set_xticks([0, 2, 4, 6])
  
# setting ticks for y-axis
ax.set_yticks([-1, 0, 1])
  
# setting label for y tick
ax.set_yticklabels(["sin(-90deg)", "sin(0deg)", "sin(90deg)"])
  
plt.show()


Output:



Similar Reads

How to Hide Axis Text Ticks or Tick Labels in Matplotlib?
The Matplotlib library by default shows the axis ticks and tick labels. Sometimes it is necessary to hide these axis ticks and tick labels. This article discusses some methods by which this can be done. Ticks: The axes' points are marked with ticks, which are also known as little geometrical scale lines.Tick labels: They are the name given to the t
4 min read
Rotate axis tick labels in Seaborn and Matplotlib
Seaborn and Matplotlib both are commonly used libraries for data visualization in Python. We can draw various types of plots using Matplotlib like scatter, line, bar, histogram, and many more. On the other hand, Seaborn provides a variety of visualization patterns. It uses easy syntax and has easily interesting default themes. It specializes in sta
3 min read
How to Set Tick Labels Font Size in Matplotlib?
Prerequisite: Matplotlib In this article, we will learn how to change (increase/decrease) the font size of tick label of a plot in matplotlib. For this understanding of following concepts is mandatory: Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization libr
2 min read
Rotation of colorbar tick labels in Matplotlib
Colorbar is an axis that indicates the mapping of data values to the colors used in plot. The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale. [caption width="800"]Typical Colorbar[/caption] Sometimes it is desirable to rotate the ticklabels for better visualization and understanding. To chang
3 min read
Python - Convert Tick-by-Tick data into OHLC (Open-High-Low-Close) Data
In this post, we'll explore a Python pandas package feature. We frequently find queries about converting tick-by-tick data to OHLC (Open, High, Low and Close). Using pandas kit this can be done with minimum effort. The OHLC data is used over a unit of time (1 day, 1 hour etc.) to perform a technical analysis of price movement. The First Step: The f
2 min read
Python | Matplotlib.pyplot ticks
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2003.One of the greatest benefits of visualization is that it allows us visual access to huge
3 min read
Change the x or y ticks of a Matplotlib figure
Matplotlib is a plotting library in Python to visualize data, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. Pyplot is a module within the Matplotlib library which is a shell-like interface to Matplotlib module. There are many ways to change the interval of ticks of axes of a plot of M
5 min read
How to Remove Ticks from Matplotlib Plots?
Matplotlib is a Python library that offers us various functions using which we can plot our data and visualize it graphically. But often plotting a graph using the Matplotlib library, we get ticks in our plot which are marked by default on both sides of the plot on the x and y-axis. There can be cases when we don't want to show these ticks in our p
5 min read
How to Change the Number of Ticks in Matplotlib?
In this article, we will see how to change the number of ticks on the plots in matplotlib in Python. Method 1: Using xticks() and yticks() xticks() and yticks() is the function that lets us customize the x ticks and y ticks by giving the values as a list, and we can also give labels for the ticks, matters, and as **kwargs we can apply text effects
5 min read
Change the label size and tick label size of colorbar using Matplotlib in Python
In this article, we will learn how to change the label size and tick label size of colorbar in Matplotlib using Python. Labels are a kind of assigning name that can be applied to any node in the graph. They are a name only and so labels are either present or absent. To properly label a graph, helps to identify the x-axis and y-axis. Each tick mark
2 min read
Article Tags :
Practice Tags :