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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63bf65654c702210899906/] [crayon-63bf65654c70d374253982/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63bf65654c9cf258206148/] [crayon-63bf65654c9d6239543004/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63bf65654cbcb650787416/] [crayon-63bf65654cbd1694359468/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

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.