How to Change the Font Size of the Title in Matplotlib Figure?


Python being a versatile programming language supports a wide range of libraries for various applications. It supports multiple programming paradigms, including structured, object-oriented, and functional programming. In this article, we will learn about matplotlib, how to create a graph using this library and then how to change the font size of the title in matplotlib figure by various methods.

What is Matplotlib?

Matplotlib is a multi-platform data visualization library. It is a full library for creating an animated and interactive visualization in python. It is an amazing visualization library in python for 2D and 3D plots of the array.

It has a module named “pyplot” which makes things easy for plotting by providing features to control line styles, font properties, formatting axes, etc. It can also integrate with other libraries such as numpy which is a library known for numerical computing in python.

We can install matplotlib by using a command “pip install matplotlib” in the command prompt.

What is pyplot?

Pyplot is an Application Programming Interference (API) in python matplotlib. It is a submodule of the matplotlib library for python. It was introduced by john hunter in 2002. It is a library consisting of a collection of functions/methods used for plotting simple 2D graphs using python.

This provides a “MATLAB” interface used to create a figure, create a plot line area, decorates the plot with labels, etc. It is used to create many varieties of plots like line plot, histograms, scatter, bars, 3D plots, images, contours, and polar. Matplotlib is an open–source drawing library that supports various drawing types.

How to Create a Graph in Python?

Matplotlib is used to draw a graph in python by using different statements such as for line graph we have to use ‘plt.plot’, for bar graph ‘plt.bar’, for scatter graph ‘plt.scatter’. A graph helps in analysing data easily and in a more engaging way.

Example

Here, in this program we have plotted a line graph using plot() function of matplotlib.

import matplotlib.pyplot as plt
player =["rahul","kunal","knuj","pearl","yug","heamnt"]
goals =[21,30,12,6,19,0]
plt.plot(player, goals, color='r')
plt.title("FOOTBALL MATCH")
plt.xlabel("PLAYERS")
plt.ylabel("GOALS")
plt.show()

Output

We have three different methods of changing the font size of the title in matplotlib.

Using the title() Function

Pyplot.title() is used to assign title to a plot and also helps to change the font size of graphs title.

Example

In the following example we are creating a line plot representing the weather report of a particular area throughout the week. Using the title() function we have created a title of the plot saying “weather report” with the font size 40.

import matplotlib.pyplot as plt
days =["mon","tue","wed","thrus","fri","sat","sun"]
temp =[97.9,78.9,67.5,90.7,56.0,89.7,45.08]
plt.plot(days, temp, color='g')
plt.title("WEATHER REPORT", fontsize=40)
plt.xlabel("DAYS")
plt.ylabel("TEMPERATURE")
plt.show()

Output

Using the set_size() Function

The set_size() is used to change the plot’s title fontsize. It can be used for increasing and decreasing the size of title font.

Example

import matplotlib.pyplot as plt
players= ["umang", "abhinav", "arry", "linda", "denny", "alis", "kunnal"]
medals =[34,56,78,90,65,54,96]
plt.plot(players ,medals, color='y')
a=plt.gca()
plt.title("IPL TOURNAMENT")
plt.xlabel("PLAYER")
plt.ylabel("MEDALS")
a.title.set_size(10)
plt.show()

Output

Using the suptitle() Function

The suptitle() function in pyplot module of the matplotlib library is used to add a title to a figure and change the fontsize of title.

Example

Following is an example –

import matplotlib.pyplot as plt
items= ["chocolate", "toffees", "cake", "colddrinks", "lays", "biscuit", "frooti"]
sales =[89000, 23000, 98777, 12000, 65000, 11100, 96000]
plt.plot(items,sales,color='m')
a=plt.gca()
plt.xlabel("ITEMS")
plt.ylabel("SALES")
plt.suptitle("SHOP MANGEMENT", fontsize =15)
plt.show()

Output

Conclusion

In this article, we have briefly discussed about the different methods used to change fontsize of the title of graph plot in matplotlib. Presence of a range of tools makes it a highly customizable library which can be used to create complex visualizations with variety of axes and sub-plots.

Updated on: 11-Oct-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements