Thursday, 21 September 2023

How to Set a Title to a Plot?

Using plot.title() method, we can add title to a plot.

 

Example

plt.title('India Population')

 

plot_title.py

import matplotlib.pyplot as plt

# Sample data
year = [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
population = [1246.2, 1262.6, 1279.7, 1297.4, 1315.8, 1334.9, 1354.7, 1374.2, 1407.6, 1417.2]

# Create a line plot
plt.plot(year, population)

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Population in Millions')
plt.title('India Population')

# Display the plot
plt.show()

 

Output


Set font size, weight and color to the title

 

plt.title('India Population', fontsize=16, color='blue', fontweight='bold')

 

In the above example, I set the title font size to 16 points, the title color to blue, and the title font weight to bold.

 

plot_title_font_customization.py

import matplotlib.pyplot as plt

# Sample data
year = [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
population = [1246.2, 1262.6, 1279.7, 1297.4, 1315.8, 1334.9, 1354.7, 1374.2, 1407.6, 1417.2]

# Create a line plot
plt.plot(year, population)

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Population in Millions')
plt.title('India Population', fontsize=16, color='blue', fontweight='bold')

# Display the plot
plt.show()

Output


 


Set fontname and background color to the title

plt.title('India Population', fontname='Arial', backgroundcolor='lightblue', fontsize=16, color='blue', fontweight='bold')

 

In the above example, I set the fontname 'Arial', which changes the font family of the title to Arial and the backgroundcolor parameter is set to 'lightblue', which gives the title a light blue background.

 

plot_title_fontname.py


import matplotlib.pyplot as plt

# Sample data
year = [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
population = [1246.2, 1262.6, 1279.7, 1297.4, 1315.8, 1334.9, 1354.7, 1374.2, 1407.6, 1417.2]

# Create a line plot
plt.plot(year, population)

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Population in Millions')
plt.title('India Population', fontname='Arial', backgroundcolor='lightblue', fontsize=16, color='blue', fontweight='bold')

# Display the plot
plt.show()

Output



Customize title alignment

Using verticalalignment and horizontalalignment parameters, we can control the alignment of the title text within the title box.

 

‘verticalalignment’ can take one of the below values.

a.   bottom

b.   center

c.    top

 

‘horizontalalignment’ can take one of the below values.

a.   right

b.   center

c.    left

 

plot_title_alignment.py

import matplotlib.pyplot as plt

# Sample data
year = [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022]
population = [1246.2, 1262.6, 1279.7, 1297.4, 1315.8, 1334.9, 1354.7, 1374.2, 1407.6, 1417.2]

# Create a line plot
plt.plot(year, population)

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Population in Millions')
plt.title('India Population in millions by year wise', verticalalignment='center',
          horizontalalignment='center',  color='blue', fontweight='bold',
          backgroundcolor='lightblue')

# Display the plot
plt.show()

Output




Previous                                                    Next                                                    Home

No comments:

Post a Comment