Change the font size of title in Matplotlib

In Python, there are a lot of libraries that are used for data visualization. One of the libraries is the Matplotlib library. The Matplotlib library is a very good library when it comes to data visualisation in python. With the help of this library, many types of graphs and visual representations of the data can be made in Python.

While plotting graphs with the help of this library, it is always necessary to label everything on the graph and give the graph a proper title. This helps in understanding all the visualizations and graphs more efficiently.

In this tutorial, we will see how to add and modify the font of the title in Matplotlib library.

Use the matplotlib.pyplot.title() Function

The matplotlib.pyplot.title() function of the matplotlib library is used to assign the title to a given visualization or graph. This function has various parameters such as:

  • label (str) – This parameter is used to defines the text that is used for the title of the visualization.

  • fontdict (dict) – This parameter is a dictionary that is used to control the appearance of the text of the title. The default fontdict is:

  • loc (str) – This parameter defines the place of the title. There are three places for the title, left, right, and centre. The default value of this parameter is centre. This is an optional parameter i.e, it is not necessary to define it in every program.

Now let’s see how to use the matplotlib.pyplot.title() function with help of some examples:

Example 1:

Output:

  • In the above code, a straight line is plotted with the help of the formula y=mx+c. Note that to define the x coordinate, the linspace function of the NumPy module is used. The linspace() function helps in creating a random numerical sequence that are evenly spaced.
  • Also, note that the font sizes of the x and y label can also modify as per requirement

Example 2:

Output:

  • Note that in the given code, all the different components are named with the of labels parameter of the pie function of the matplotlib library which is defined in the starting itself.
  • Finally, the title is defined with a proper font size with centre as its default loc.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

  • Python print to file
    17 November

    How to Print to File in Python?

    Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.