Matplotlib Remove Ticks

Matplotlib Remove Ticks: Methods to remove Both xticks and yticks

GET FREE AMZAON AUDIOBOOKS

When you plot a figure using the matplotlib library then by default ticks are labelled for both x-ticks and the y-ticks. Suppose you want to remove them then how you can do so ? In this entire tutorial you will know the various methods to implement Matplotlib Remove Ticks.

Various methods to remove ticks in Matplotlib

In this section, you will know all the methods to implement Matplotlib Remove Ticks. Firstly you will know to remove the x-ticks and after that y-ticks. Finally, you will learn how to remove both of them using a single line of code.

Please note that all the coding parts have been done on Jupyter notebook. So it’s better that you must do the entire work on Notebook for deep understanding.

Remove xticks in matplotlib

Here you will learn two methods for removing xticks. The first method is using the plt.tick_params() method and the other is plt.xticks() function. Let’s do it.

Method 1: Using the plt.tick_params()

There are certain parameters you have to use inside the ticks_params() method like axis, left, labelleft to manipulate the ticks. Execute the below lines of code to remove xticks.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data.plot(figsize=(20,10))
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False) # labels along the bottom edge are off

Here I have used %matplotlib inline to display the image inline. I have used the forex pair dataset you can download by clicking the button below. The  figsize=(20,10) is used to change the size of the image. When you will run the above code you will get the following output.

Output

Use the plt.tick_params() to remove xticks
Use the plt.tick_params() to remove xticks

You can see the xticks removed from the x-axis.

Method 2: Reomve xticks using plt.xticks()

The second method to remove xticks is using the plt.xticks() method. This method accepts two major parameters ticks and labels as a list. To remove x ticks just pass the blank list for both arguments. Run the below lines of code and see the output.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data.plot(figsize=(20,10))
plt.xticks([], [])

Output

Use the plt.xticks() to remove xticks
Use the plt.xticks() to remove xticks

In the next section, you will know to Remove xticks in matplotlib using the above same methods.

Remove yticks in Matplotlib

Method 1: Using the plt.tick_params()

The implementation is the same as the above. Here you have to tweak some parameters inside the tick_params() function. Execute the below line of code and see the output.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data.plot(figsize=(20,10))
plt.tick_params(
axis='y', # changes apply to the x-axis
which='both', # both major and minor ticks are affected, # ticks along the bottom edge are off
left=False, # ticks along the top edge are off
labelleft=False,) # labels along the bottom edge are off

Output

Use the plt.tick_params() to remove yticks
Use the plt.tick_params() to remove yticks

Method 2: Remove yticks using plt.yticks()

The second method to remove yticks is passing the two empty list as an argument for the function plt.yticks(). Run the below code and see the output.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data.plot(figsize=(20,10))
plt.yticks([], [])

Output

Use the plt.yticks() to remove yticks
Use the plt.yticks() to remove yticks

You can see the sticks have been successfully removed.

 

These are methods to remove xticks and yticks from the figure using the Matplotlib package. Sometimes you want to remove both the ticks at once. To do so you have to use the plt.axis(‘off’). It will remove both the ticks labels. In fact, it will remove the axis.

Hope this tutorial has solved your queries on removing ticks in Matplotlib. If you have any queries then you can contact us for more help.

 

Source:

matplotlib.axes.Axes.tick_params

matplotlib.pyplot.xticks

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner