Customization of Ticks in Matplotlib.pyplot

In this tutorial, we will discuss the customization of ticks in matplotlib.pyplot which clearly means to modify or customize the ticks in the way we want to. Ticks are values that are used to show the points on axes, when we plot a graph, it automatically adjusts axes and takes default ticks which are sufficient but inefficient when customization is needed.

Default ticks in Matplotlib.pyplot

Let’s first try to plot the default graph by not making any changes in ticks. So this is how we will plot a graph with some default values-

import matplotlib.pyplot as plt

x = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500]
y = [1, 4, 3, 2, 7, 6, 9, 8, 10, 5]

plt.plot(x, y)

Default ticks in Matplotlib.pyplot

Customize ticks in Matplotlib

Now we will customize the ticks as per our needs, for example, we need to hide the marks on the axes, so we will now plot a graph with ax1 as tick params with parameters as an axis as both and which as both and length could be in floating value. There are so many other parameters in tick_params(). The second customization could be hiding the values on the axes as well, for this the code could be designed in a way to set visible as False.

Here’s a snippet of the code-

import random
import matplotlib.pyplot as plt

fig = plt.figure()


ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)

ax1.plot(x, y)
ax1.tick_params(axis ='both', which ='both', length = 0)


Customize ticks in Matplotlib

So here we have customized the ticks in the way we wanted.

Changing the range of ticks – Pyplot

In the above example of the implementation of the customization of ticks, we changed the values and marks on the axis. Now we can customize it in a way to change the range of ticks.

Let’s implement this through an example-

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

plt.plot(x, y )
plt.xticks(np.arange(0, 8, 2))
plt.yticks(np.arange(0, 110, 10))

Customization of Ticks in Matplotlib.pyplot

So here we have changed the range of ticks using matplotlib in Python.

Thanks for reading the tutorial! I hope this tutorial was helpful in your task to customize the ticks.

Leave a Reply

Your email address will not be published. Required fields are marked *