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.
- xticks(ticks=None, labels=None, **kwargs)– used to get and set the current tick locations and labels of the x-axis.
- yticks(ticks=None, labels=None, **kwargs)- used to get and set the current tick locations and labels of the y-axis.
- set_visible(boolean)- sets visibility
Hiding tick labels
The functions xticks() and yticks() are used to denote positions using which a data point is supposed to be displayed. They take a list as argument. Thus, axis text ticks or tick labels can be disabled by setting the xticks and yticks to an empty list as shown below:
By default, in matplotlib library, plots are plotted on a white background. Therefore, setting the color of tick labels as white can make the axis tick labels as hidden. For this only color attribute needs to passed with w (represents white) as a value to xticks() and yticks() function. Implementation is given below:
Null Locator is a type of tick locator which makes the axis ticks and tick labels disappear. Simply passing NullLocator() function will be enough.
Observe the syntax of xticks() and yticks() carefully, if even just a space is passed to them along with the data the output will be our desired result.
This method is just a modification of method 4. Instead of passing an empty string, assign label as space in the functions itself.
Using set_visibile() we can also set visibility of tick labels as False, that will not make them appear in our plot. This method hides labels as well as ticks, so if some requirement needs ticks to be displayed this isn’t the option to for, multiple methods shown above would stand ideal though.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course