Formatting Axes in Python-Matplotlib Last Updated : 22 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Matplotlib is a python library for creating static, animated and interactive data visualizations. Note: For more information, refer to Introduction to Matplotlib What is Axes? This is what you think of as 'plot'. It is the region of the image that contains the data space. The Axes contains two or three-axis(in case of 3D) objects which take care of the data limits. Below is an image illustrating the different parts of a figure which contains the graph. The different aspects of the Axes can be changed according to the requirements. 1. Labelling x, y-Axis Syntax: for x-axis Axes.set_xlabel(self, xlabel, fontdict=None, labelpad=None, \*\*kwargs) for y-axis Axes.set_ylabel(self, ylabel, fontdict=None, labelpad=None, \*\*kwargs) These functions are used to name the x-axis and y-axis. Example: Python3 1== # importing matplotlib module import matplotlib.pyplot as plt import numpy as np # x-axis & y-axis values x = [3, 2, 7, 4, 9] y = [10, 4, 7, 1, 2] # create a figure and axes fig, ax = plt.subplots() # setting title to graph ax.set_title('Example Graph') # label x-axis and y-axis ax.set_ylabel('y-AXIS') ax.set_xlabel('x-AXIS') # function to plot and show graph ax.plot(x, y) plt.show() Output: 2. Limits of x, y-Axis Syntax: For x-axis: Axes.set_xlim(self, left=None, right=None, emit=True, auto=False, \*, xmin=None, xmax=None) Parameters: left and right - float, optional The left xlim(starting point) and right xlim(ending point) in data coordinates. Passing None leaves the limit unchanged. auto - bool or None, optional To turn on autoscaling of the x-axis. True turns on, False turns off (default action), None leaves unchanged. xmin, xmax : They are equivalent to left and right respectively, and it is an error to pass both xmin and left or xmax and right. Returns: right, left - (float, float) For y-axis: Axes.set_ylim(self, bottom=None, top=None, emit=True, auto=False, \*, ymin=None, ymax=None) Parameters: bottom and top - float, optional The bottom ylim(starting point) and top ylim(ending point) in data coordinates. Passing None leaves the limit unchanged. auto - bool or None, optional To turn on autoscaling of the y-axis. True turns on, False turns off (default action), None leaves unchanged. ymin, ymax : They are equivalent to left and right respectively, and it is an error to pass both ymin and left or ymax and right. Returns: bottom, top - (float, float) Example: Python3 1== import matplotlib.pyplot as plt import numpy as np x = [3, 2, 7, 4, 9] y = [10, 4, 7, 1, 2] # create a figure and axes fig, ax = plt.subplots() ax.set_title('Example Graph') ax.set_ylabel('y-AXIS') ax.set_xlabel('x-AXIS') # set x, y-axis limits ax.set_xlim(0, 10) ax.set_ylim(0, 10) # function to plot and show graph ax.plot(x, y) plt.show() Output: 3. Major and Minor Ticks The Ticks are the values/magnitude of the x and y axis. Minor ticks are divisions of major ticks. There are two classes Locator and Formatter. Locators determine where the ticks are and Formatter controls the formatting of the ticks. These two classes must be imported from matplotlib. MultipleLocator() places ticks on multiples of some base. FormatStrFormatter uses a format string (e.g., '%d' or '%1.2f' or '%1.1f cm' ) to format the tick labels. Note: Minor ticks are OFF by default and they can be turned ON by without labels by setting the minor locator and minor tick labels can be turned ON by minor formatter. Example: Python3 1== # importing matplotlib module and respective classes import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import (MultipleLocator, FormatStrFormatter, AutoMinorLocator) x = [3, 2, 7, 4, 9] y = [10, 4, 7, 1, 2] fig, ax = plt.subplots() ax.set_title('Example Graph') ax.set_ylabel('y-AXIS') ax.set_xlabel('x-AXIS') ax.set_xlim(0, 10) ax.set_ylim(0, 10) # Make x-axis with major ticks that # are multiples of 11 and Label major # ticks with '% 1.2f' formatting ax.xaxis.set_major_locator(MultipleLocator(10)) ax.xaxis.set_major_formatter(FormatStrFormatter('% 1.2f')) # make x-axis with minor ticks that # are multiples of 1 and label minor # ticks with '% 1.2f' formatting ax.xaxis.set_minor_locator(MultipleLocator(1)) ax.xaxis.set_minor_formatter(FormatStrFormatter('% 1.2f')) ax.plot(x, y) plt.show() Output: Comment More infoAdvertise with us Next Article Formatting Axes in Python-Matplotlib A akshay_sharma08 Follow Improve Article Tags : Python Python-matplotlib Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like