Introduction to Python Matplotlib Labels & Title

In This Article, You Will Learn About Python Matplotlib Labels & Title.

Python Matplotlib Labels – Before moving ahead, let’s take a look at Python Matplotlib Line

Table of Contents

Create Labels

To put the label name of each axis (x-axis and y-axis), we can use xlabel() function and ylabel() function.

Example: Putting the x-axis and y-axis label names.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([2005, 2006, 2007, 2008, 2009])

plt.plot(x_axis, y_axis)
plt.xlabel('Data')
plt.ylabel('year')

plt.show()

				
			
python-matplotlib-labels

As shown above, it returned a graph with the name of the x-axis and y-axis.

Create a Title

To put the Title name of the graph, we can use the title() function.

Example: Putting the title name.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([2005, 2006, 2007, 2008, 2009])

plt.plot(x_axis, y_axis)
plt.title('Record')
plt.xlabel('Data')
plt.ylabel('year')

plt.show()

				
			
python-matplotlib-labels

As a result, it returned a graph with the name of the x-axis, y-axis, and title.

Set Font Properties for Title and Labels

To set the font of the title and labels, we can use fontdict as a parameter in the title(), xlabel(), and ylabel().

Example: Putting the font name.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([2005, 2006, 2007, 2008, 2009])

first_font = {'family': 'Arial', 'color': 'green', 'size': '10'}
second_font = {'family': 'Arial', 'color': 'red', 'size': '15'}

plt.plot(x_axis, y_axis)
plt.title('Record', fontdict=second_font)
plt.xlabel('Data', fontdict=first_font)
plt.ylabel('year', fontdict=first_font)

plt.show()

				
			
python-matplotlib-labels

As shown above, it returned a graph with adding font properties in the x-axis, y-axis, and title.

Position the Title

To set the location of the title, we can pass loc as a parameter in the title() function.

The valid values are – ‘left’, ‘center’, and ‘right’. By default, the value is set to ‘center.’ 

Example: Put the title name on the left.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([2005, 2006, 2007, 2008, 2009])

plt.plot(x_axis, y_axis)
plt.title('Record', loc = 'left')
plt.xlabel('Data')
plt.ylabel('year')

plt.show()

				
			
python-matplotlib-labels

As shown above, it returned a graph setting the location of the title in the left to align.

Now, passing loc with font properties.

Example: Putting the title name in ‘left’ with font properties.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([2005, 2006, 2007, 2008, 2009])

first_font = {'family': 'Arial', 'color': 'green', 'size': '10'}
second_font = {'family': 'Arial', 'color': 'red', 'size': '15'}

plt.plot(x_axis, y_axis)
plt.title('Record', loc = 'left', fontdict=second_font)
plt.xlabel('Data', fontdict=first_font)
plt.ylabel('year', fontdict=first_font)

plt.show()

				
			
python-matplotlib-labels

As shown above, it returned a graph with the title’s location in the left to align and add font properties in the x-axis, y-axis, and title.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Connect on:

1 thought on “Introduction to Python Matplotlib Labels & Title”

Leave a Comment

Stay in the loop

codingstreets