# Matplotlib Labels and Title

In this article, we will explore how to add Matplotlib labels and titles to our plots with examples.

## Adding Labels to a Plot

Labels are used to identify the x and y-axis of a plot. The x-axis represents the independent variable, while the y-axis represents the dependent variable.

In Matplotlib, we can add labels to our plot using the xlabel() and ylabel() functions, respectively.

Label the x axis as ‘Pulse Rate’ and y axis as ‘Heart Rate‘ as follows:

#### Example:

import numpy as npy import matplotlib.pyplot as pt pulse_rate = npy.array([61, 68, 73, 77, 82]) heart_rate = npy.array([80, 75, 88, 69, 91]) pt.plot(pulse_rate, heart_rate) pt.xlabel("Pulse Rate") pt.ylabel("Heart Rate") pt.show()

First insert the marker and color into the following example then invoke the labels of the x and y axis:

#### Example:

import numpy as npy import matplotlib.pyplot as pt pulse_rate = npy.array([61, 68, 73, 77, 82]) heart_rate = npy.array([80, 75, 88, 69, 91]) pt.plot(heart_rate, pulse_rate, marker = 'o', c = 'Black') pt.xlabel("Heart Rate") pt.ylabel("Pulse Rate") pt.show()

## Adding a Title to a Plot

A title provides a brief description of the plot and helps to convey the main idea.

According to Matplotlib labels, you can invoke the title() function in Pyplot to generate a title for the plot.

In the below example, we apply the title() function to insert a title:

#### Example:

import numpy as npy import matplotlib.pyplot as pt pulse_rate = npy.array([61, 68, 73, 77, 82]) heart_rate = npy.array([80, 75, 88, 69, 91]) pt.plot(pulse_rate, heart_rate) pt.title("MRX MEDICARE") pt.xlabel("Pulse Rate") pt.ylabel("Heart Rate") pt.show()

Utilizing the title function invokes the title ‘HEART & PULSE RATE’:

## Customizing Labels and Titles

Matplotlib provides various customization options to modify the appearance of the labels and titles.

We can change the font size, font weight, font family, and color of the labels and titles using different parameters.

## Set Font Properties for Title and Labels

There are several methods you can apply to configure font properties for the title and labels in Matplotlib.

These methods include xlabel(), ylabel(), and title(). You can invoke the fontdict parameter in xlabel(), ylabel(), and title().

For the title and labels, configure the font attributes as follows:

#### Example:

import numpy as npy import matplotlib.pyplot as pt pulse_rate = npy.array([61, 68, 73, 77, 82]) heart_rate = npy.array([80, 75, 88, 69, 91]) font_style1 = {'family':'monospace','weight':'bold','color':'m', 'size':15.5} font_style2 = {'family':'fantasy','weight':'heavy','color':'Maroon', 'size':10.5} pt.title("MRX MEDICARE", fontdict = font_style1) pt.xlabel("Pulse Rate", fontdict = font_style2) pt.ylabel("Heart Rate", fontdict = font_style2) pt.plot(pulse_rate, heart_rate) pt.show()

Dedicate each font dictionary to each function:

## Title Position

In title(), you can align the name by passing the loc parameter.

There are three acceptable values: ‘left‘, ‘right‘, and ‘center‘. It is assigned to ‘center’ by default.

Right align the title in the following example:

#### Example:

import numpy as npy import matplotlib.pyplot as pt pulse_rate = npy.array([61, 68, 73, 77, 82]) heart_rate = npy.array([80, 75, 88, 69, 91]) font_style1 = {'family':'monospace','weight':'bold','color':'m', 'size':15.5} font_style2 = {'family':'fantasy','weight':'heavy','color':'Maroon', 'size':10.5} pt.title("MRX MEDICARE", fontdict = font_style1, loc = 'right') pt.xlabel("Pulse Rate", fontdict = font_style2) pt.ylabel("Heart Rate", fontdict = font_style2) pt.plot(pulse_rate, heart_rate) pt.show()

Align the title to the left and also insert the background color:

### Example Explanation

In above example of plotting a graph using the numpy and matplotlib libraries in Python. It shows the relationship between pulse rate and heart rate.

The numpy library is used to create two arrays, pulse_rate and heart_rate, which contain five values each. The values in the pulse_rate array represent the pulse rate in beats per minute, while the values in the heart_rate array represent the heart rate in beats per minute.

The matplotlib library is used to create a graph of the relationship between the two arrays. The pt.title() function is used to add a title to the graph, and the font_style1 variable is used to define the font style of the title. The pt.xlabel() and pt.ylabel() functions are used to add labels to the x and y axes of the graph, respectively, and the font_style2 and font_style3 variables are used to define the font styles of the labels.

The pt.plot() function is used to plot the values in the heart_rate array against the values in the pulse_rate array, and the marker and c parameters are used to define the style and color of the data points on the graph. Finally, the pt.show() function is used to display the graph.

## Matplotlib Labels & Title Benefits

Matplotlib labels and title play an important role in creating effective and informative visualizations. The following benefits can be associated with their use:

• Matplotlib labels and title provide clear information about the data represented on the graph. The title summarizes the visualization while the x and y-axis labels provide context about the data.
• Properly labeled graphs enhance communication and help the reader understand the message effectively. A well-labeled graph can convey the intended message to the reader in a clear and concise manner.
• Matplotlib labels and title improve the visual appeal of the graph. Consistent fonts and colors make the graph more aesthetically pleasing and easier to read.
• Matplotlib labels and title can also make the visualization more accessible to individuals with visual impairments. Screen readers can read the labels and title to users who may have difficulty interpreting visual information.

### Conclusion

To summarize, Matplotlib labels and title are essential in creating effective and informative visualizations. They serve multiple purposes, such as providing clarity about the data represented on the graph and enhancing communication by conveying the intended message clearly and concisely. Properly labeled graphs also give a professional appearance and are aesthetically pleasing, making them easier to read. Furthermore, the use of labels and title can improve accessibility for individuals with visual impairments.

In summary, it is crucial to include labels and title in Matplotlib to create visually appealing and accessible visualizations that effectively communicate the intended message to the reader.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0