Python

# Matplotlib figure title

Matplotlib is a NumPy-based visual analytics package. In matplotlib, the plt.title() function defines the label of the plot being created and presents it using multiple parameters. Let’s discuss plt.title() function in detail.

## Use pyplot.title() function to show ReLU function plot:

We utilize Matplotlib.pyplot to visualize a ReLU function chart and matplotlib.pyplot.title() function is used to create the title of the graph.

import matplotlib.pyplot as plt
a = [-10,-8,-6,-4,-2,0,2,4, 6, 8, 10]
b = []
for i in range(len(a)):
b.append(max(0,a[i]))
plt.plot(a, b, color='red')
plt.xlabel('X-AXIS')
plt.ylabel('Y-AXIS')
plt.title(label="ReLU function chart",
fontsize=44,
color="red")

At the beginning of the program, we would integrate the package matplotlib.pyplot as a plot. This library is applied for graphic representation. Now we have to assign data points for both the x-axis and y-axis.

We declare two arrays, which are stored in two different variables. The first array contains the elements for the X-axis and the second array is an empty array.

Along with that, we employ the ‘for’ loop. We form a new variable inside the for a loop. Inside for loop, we have to find out the length of the x array by calling the len() function. We determine the maximum value of the data points of the ‘a’ array.

Now we apply plt.plot() to illustrate the graph. This function has three parameters, including two separate arrays made to data points of axes. And the ‘color’ parameter is also provided for this function. This ‘color’ shows the color of the line. In the next step, we call the function ply.label() for specifying the tags to the x-axis and y-axis.

Lastly, we define the plt.title() function, which is used to show the graph’s title. We give the ‘label’ parameter, ‘fontsize’ parameter, and ‘color’ parameter to this function. These arguments represent the caption, fontsize, and color of the plot.

## Specify the font attributes for the title of the graph:

Numerous features of the font for different plot labels can be specified. We just utilize the fontdict argument in the xlabel() function, ylabel() function, and title() function to adjust font parameters for the labels.

import matplotlib.pyplot as plt
import numpy as np
a = np.array([100, 110, 120, 130, 140, 150, 160, 170, 180, 190])
b = np.array([200, 210, 220, 230, 240, 250, 260, 270, 280, 290])
font = {'family':'Arial','color':'green','size':30}
font1 = {'family':'Times Roman','color':'blue','size':22}
plt.title("Figure", fontdict = font)
plt.xlabel("X-AXIS", fontdict = font1)
plt.ylabel("Y-AXIS", fontdict = font1)
plt.plot(a, b)
plt.show()

After importing the matplotlib and NumPy libraries, we create two variables. Then we use the NumPy library to declare two arrays having the data sets of x and y axes. These arrays are stored in variables ‘a’ and ‘b.’ The function np.array() is called for this purpose.

Now we define different properties of the font for the label. For the first font, we set Arial as a family, green as a color, and 30 as a size of the font. Similarly, we specify properties for the second font. The family parameter is Times Roman, the color parameter is blue, and the size parameter is 22. These font properties are stored in variables termed ‘font’ and ‘font1’.

Now it’s time to call the plt.title() function. Fontdict is used to show which font properties are given to the graph’s title. Similarly, we define the plt.label() function for the x-axis and y-axis. The title for the x-axis and y-axis is passed as a parameter. And we also choose the font properties for the label of both axes. Now to draw and display the graph, we apply separate functions.

## Add location to the label of the Graph:

Here we can utilize the ‘loc’ parameter to define the location of the label of the plot.

import matplotlib.pyplot as plt
l = [10, 20, 30]
m = [25, 35, 45]
plt.plot(l, m)
plt.title('Graph', fontsize=36, loc='right')

First, we introduce matplotlib.pyplot as plt for graphic visualizations. Next, we define the data sets for the X-axis and Y-axis. These data sets are stored in ‘l’ and ‘m’ variables, respectively. We give three values to each axis.

For the creation of the graph, we call plt.plot() function. The X-axis and Y-axis data sets are provided as an argument for this function. We apply the plt.title() function to insert the title. This function holds three parameters. The ‘label,’ fontsize and ‘loc’ which we want to give to the graph’s title are specified by these parameters.

## Add sup title above the title of the graph:

In this step, we insert a sup title just above the title of the figure. The location of the sup title is specified by the ‘y’ argument of the suptitle() method.

import matplotlib.pyplot as plt
import numpy as np
a = np.random.normal(size=40000)
b = a * 4 + np.random.normal(size=40000)
plt.hist2d(a, b, bins=(20, 20), cmap=plt.cm.Reds)
plt.suptitle("2D Graph \n", fontsize=28, y=1.1)
plt.title("Graph with a red colour scheme", color="purple", style='oblique'
plt.show()

We have to utilize the matplotlib.pyplot as plt and NumPy as np libraries in this instance. These libraries are introduced for their specific purposes of visualization and mathematical analysis. We defined the x and y axes data by using the NumPy library.

We also specified the size separately with the help of np.random.normal() function. A two-dimensional histogram is being produced by calling the plt.hist2d () method. This function contains four different arguments, including the value of the x and y axes.

Furthermore, we have to insert the sup title for the graph. Hence for inserting this we utilize the plt.suptitle() function. We pass the label as a parameter, fontsize of the label, and the ‘y’ argument to this function. Here ‘y’ argument represents the position of the sup title.

So we can amend the location of the sup title by our choice. The sup title is situated just above the main title of the graph. Now we insert the basic tag for the plot by calling the plt.title() function. The ‘color’ parameter and ‘style’ parameter are provided for this function.

Hence we can modify the color and style of the label. After all this, we get the plot by applying the plt.show() function.

## Conclusion:

In this guide, we have enlightened how to use the pyplot.title() function with numerous instances. We examined the utilization of this function to represent the graph with the ReLU function. We also see how to specify the elements of font defined for the labels of the figure. In the end, we see the method of adding the ‘location’ to the caption of the plot.