Matplotlib title font size

In this Python tutorial, we will discuss Matplotlib title font size in python. Here we will cover different examples related to title font size using matplotlib. And we will also cover the following topics:

  • Matplotlib title font size
  • Matplotlib set title font size
  • Matplotlib title font size bold
  • Matplotlib legend title font size
  • Matplotlib subplot title font size
  • Matplotlib rcParams title font size
  • Matplotlib pie chart title font size
  • Matplotlib bar chart title font size
  • Matplotlib colorbar title font size
  • Matplotlib figure title font size
  • Matplotlib set_title text font size
  • Matplotlib title different font size

Matplotlib title font size

Here we are going to learn about how to change the font size of the title in matplotlib in Python. Before starting the topic firstly, we have to understand what does “title” means.

Title: A name that is used to describes the plot in matplotlib.

The following steps are used to add the title to a plot are outlined below:

  • Defining Libraries: Import the important libraries which are required (For data creation and manipulation: Numpy and Pandas, For data visualization: pyplot from matplotlib).
  • Define X and Y: Define the data coordinated values used for the x-axis and y-axis.
  • Plot chart or figure: By using bar(), pie(), scatter(), plot(), etc methods we can draw a plot.
  • Add Title: By using title() method we can add titile to a chart.
  • Generate a Plot: By using show() method we can visulaize a plot.

Check out, How to install matplotlib python

Matplotlib set title font size

In Matplotlib, to set the title of a plot you have to use the title() method and pass the fontsize argument to change its font size.

The syntax to assign a title to the plot and to change its font size is as below:

# To add title
matplotlib.pyplot.title()

# To change size
matplotlib.pyplot.title(label, fontsize=None)

The above-used parameters are described as below:

  • label: specifies the title.
  • fontsize: set the font size of your choice.

Let’s see an example to set title font size:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [ 1, 2, 3, 4, 5]
y = [3, 6, 9, 12, 15]

# Plot

plt.plot(x,y,color='r')

# Define Title and change size

plt.title("Straight Line Function Chart", fontsize=10)

# Show

plt.show()
  • In the above example, we import matplotlib.pyplot library to plot a chart. After this, we define data points that are used for data plotting.
  • Then by using plt.plot() method we plot the line chart.
  • After that, we use plt.title() method to add title on the plot and we also pass the fontsize argument, set’s its value to 10.
matplotlib set title font size
plt.title() “We set font size to 10”

Read Matplotlib dashed line

Matplotlib title font size bold

Here we learn how to set the title font size to bold in Matplotlib.

The syntax to set the font size to bold:

matplotlib.pyplot.title(label, fontsize=None, fontweight=None)

The parameter used above is as below:

  • label: specifies the title of the plot.
  • fontsize: set font size of the plot.
  • fontweight: set font to bold.

Let’s have a look at an example to set the title to bold:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 15, 0.2)
y = np.sin(x)

# Plot figure

plt.plot(x, y)

# Title

plt.title("Sine Function", fontsize= 15, fontweight='bold')

# Generate Plot

plt.show()

In the above example, we use plt.title() method to add a title to the plot and we pass the fontsize and fontweight argument to the method set its value to 15 and bold respectively.

matplotlib title font size bold
” Set title font to bold “

Read Matplotlib plot_date

Matplotlib legend title font size

Here we learn to set the font size of the legend title of the plot in Matplotlib. We use the legend() method to add the legend title.

We also pass the title_fontsize argument and set its value to 30.

The syntax to add legend title and change its font-size:

# Import libraries

import matplotlib.pyplot as plt
import pandas as pd

# Define Data

df = pd.DataFrame({
    'Maths': [12, 15, 10, 3, 1, 5],
    'Science': [15, 10, 5, 4, 3, 6],
    'Computers':[20, 12, 5, 3, 5, 2]
})

labels = ['A','B','C','D','E','Fail']

# Plot stacked bar chart

ax = df.plot(stacked=True, kind='bar')
   
# Set Tick labels

ax.set_xticklabels(labels,rotation='horizontal')

ax.legend(title='SUBJECT',title_fontsize=30)

# Display chart

plt.show()
  • In the above example, we import matplotlib.pyplot, and pandas library.
  • After this, we use the pandas DataFrame() method to define labels and data coordinates and we use the plot() method to draw a stacked bar chart.
  • By using the set_xticklabels() method we set x label and also set its rotation to horizontal.
  • Then we use the ax.legend() method to set the title of the legend and pass the title_fontsize argument and set its value to 30.
matplotlib legend title font size
” Change legend title font size”

Read Matplotlib log log plot

Matplotlib subplot title font size

Here we will be going to discuss how we can change the title font size of the specific subplot if we draw multiple plots in a figure area by using the matplotlib library.

We use the set_size() method to change the title font size of a specific subplot.

Let’s understand the concept with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

# Set Title

ax[0, 0].set_title("Plot 1")
ax[0, 1].set_title("Plot 2")
ax[1, 0].set_title("Plot 3")
ax[1, 1].set_title("Plot 4")

# Change font size

ax[0,1].title.set_size(20)

# Plot graph

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4, y4)

# Display Graph

fig.tight_layout()
plt.show()
  • In the above example, we plot multiple plots in the figure area. And we want to change the title font size of the specific plot.
  • Here we use the set_title() method to add title on each of the plots.
  • We use the set_size() method with the second subplot and set its title size to 20.
matplotlib subplot title font size
” Title size of subplot 2 changed “

Read Matplotlib subplots_adjust

Matplotlib rcParams title font size

In matplotlib, the size of the title can be adjusted by changing the values of the rcParams dictionary.

We can change the default settings of ‘rc’ that are stored in a global dictionary to change its font size.

The syntax to change rcParams title font size:

parameter = {'axes.titlesize': }

plt.rcParams.update(parameter)

Here we define a parameter dictionary to change the font size of the title.

Let’s have a look at an example:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x=np.linspace(0,10,15)
y= np.cos(2 * np.pi * x)

# Set title size

par = {'axes.titlesize':30}
plt.rcParams.update(par)

# Plot

plt.plot(x, y)

# Define title and labels

plt.title('Plot of cos x')
plt.xlabel('x')
plt.ylabel('cos x')

# Show

plt.show()
  • Firstly we import matplotlib.pyplot library and numpy library.
  • After this, we define data for plotting then we use axes.titlesize and set its value to 30 and we pass this to plt.rcParams.update() method.
  • By using plt.plot() method plot a cos x graph and we also use plt.title(), plt.xlabel(), plt.ylabel() methods to set title and axes labels respectively.
  • At last, we use the show() method to visualize the graph.
matplotlib rcParams title font size
” Change rcParams title font size “

Check out, Matplotlib best fit line

Matplotlib pie chart title font size

In this section, we will learn about how to add a title to a pie chart and we will also learn to change its size and color in Matplotlib.

Example:

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

subjects = ['MATHS', 'SCIENCE', 'ENGLISH', 'HINDI', 'SOCIAL-SCIENCE']
  
data = [20, 7, 31, 25, 12]

# Creating plot

plt.pie(data, labels = subjects)

# Set title

plt.title("Popularity of subjects among students", fontsize=15, color='r', fontweight='bold')

# Show plot

plt.show()
  • Here we use plt.title() method to define the title of a pie chart and we pass label, fontsize, color and, fontweight as an argument.
  • We set the value of font size to 15, color to red, and weight to bold.
matplotlib pie chart title font size
plt.title()

Read Matplotlib subplot tutorial

Matplotlib bar chart title font size

By using the Matplotlib library, here we first plot the bar chart by using plt.bar(), and then by using plt.title() we add a title to the plot.

We set the font size to 50 and the color of the font to black.

Example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

students = [5, 6, 2, 3]
activities= ["Joging", "Gyming", "Swimming", "Shopping"]

# Plot bar chart

plt.bar( activities, students, color= 'yellow')

# Add Title

plt.title("BAR CHART", fontsize=50, color='k')

# Display chart

plt.show()
matplotlib bar chart title font size
“Bar chart”

Read Matplotlib plot bar chart

Matplotlib colorbar title font size

Here we learn to set the title on the color bar and we also learn to change its font size, color, etc. by using the Matplotlib library functionalities.

The syntax to the plot color bar and set its title text is as below:

# Plot colorbar
matplotlib.pyplot.colorbar()

# Add text title
matplotlib.pyplot.colorbar().set_label()

Let’s see an example to change color bar title font size:

# Import Library

import numpy as np
import matplotlib.pyplot as plt
  
# Define Data

subject = ['Maths', 'Science', 'Social-Science', 'Hindi', 'English']  
likes = [25, 15, 20, 16, 12]
ratio = [0.5, 1.2, 2.3, 4.3, 0.2]
  
# Plot

plt.scatter(x=subject, y=likes, c=ratio, cmap="summer")

# Color Bar
  
plt.colorbar().set_label(label='Ratio',size=25,weight='bold',color='blue')

# Show

plt.show()
  • In the above example, we first import matplotlib.pyplot, and numpy library.
  • After this, we define data and by using the plt.scatter() method we plot a scatter graph.
  • Then by using plt.colorbar() method we plot the color bar and by using set_label() we set the title of the colobar.
  • We also pass the size, weight, and color as an argument and set its value to 25, bold, and blue respectively.
matplotlib colorbar title font size
plt.colorbar()

Also, check out, What is matplotlib inline

Matplotlib figure title font size

There is a difference between the axes title and figure title. So. in this section we are going to learn about figure title.

We also called a figure title a suptitle or you can also call it a super title.

The syntax to add figure title:

matplotlib.figure.Figure.suptitle(self, t, **kwargs)

The parameters are as follow:

  • t: specifies the title text.
  • x: specifies the x location of the title.
  • y: specifies the y location of the title.
  • fontsize: specifies the font size of the text or you can also write it as size.
  • fontweight: specifies the fontweight of the text or you can also write it as weight.

suptitle() method is used to add a centered title to the figure.

Let’s see an example to understand the concept more clearly:

# Import Library

import matplotlib.pyplot as plt

# Create a new figure

fig = plt.figure()

# Add Title to figure

fig.suptitle('Grade Sheet', x= 0.5, y =1.05, fontsize=30, weight='bold') 

# Define Data

grades = ["A","B","C","D","E","Fail"]
students = [15, 12, 3, 5, 1, 2]

# Plot bar chart

plt.bar(grades,students)

# Define axes title

plt.title("Class10",fontsize=15)  

# Define axes labels

plt.ylabel("No. of students")
plt.xlabel("Grades of students")

# Display a bar chart
    
plt.show()
  • Here we use plt.figure() method to create a new figure.
  • After this, we use plt.suptitle() method to add a title to the figure and we pass the title to be written along with other arguments such as x, y. We use x and y to set location of the super title.
  • We also set the fontsize and weight to 30 and bold respectively of the figure.
  • Then we define the data to be plotted and use plt.bar() method to plot a bar chart.
  • By using the plt.title() method we add axes title and set its fontsize to 15.
  • By using plt.xlabel() and plt.ylabel() method we define axes labels. And In last, we use plt.show() method to generate a graph.
matplotlib figure title font size
fig.suptitle()

Read Python plot multiple lines using Matplotlib

Matplotlib set_title text font size

By using the set_title() method you can add a title to your plot in matplotlib.

Example:

# Import Library

import matplotlib.pyplot as plt

# Create new figure and subplot

fig = plt.figure() 
ax = fig.add_subplot(111) 

# Add title

ax.set_title('Title',fontsize= 30) 

# Axes label

ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 5.3, 6, 10, 2]

# Plot

ax.plot(x,y,'-o') 

# Show

plt.show()
matplotlib set_title text font size
set_title()

Read Matplotlib plot a line

Matplotlib title different font size

In matplotlib, we have the functionality to set the different font sizes for each of the labels in the chart.

Example:

# Import Library

import matplotlib.pyplot as plt

# Create new figure and subplot

fig = plt.figure()
ax = fig.add_subplot(111) 
 
# Add Figure Title

fig.suptitle('Figure Title', x= 0.5, y =1.06, fontsize=35) 

# Add Axes title

ax.set_title('Axes Title',fontsize= 20) 

# Add Axes label

ax.set_xlabel('x-axis', fontsize= 15)
ax.set_ylabel('y-axis', fontsize= 10)

# Define Data

x = [0, 1, 2, 3, 4, 5]
y = [1.5, 3, 6.3, 6, 10, 2]

# Plot

ax.plot(x,y,'-o') 

# Show

plt.show()

In the above example, we use the following methods to add title and set their font size as given below:

  • fig.suptitle: Add Title to figure. We set the size of the font to 35.
  • ax.set_title: Add Title to axes. We set the size of the font to 20.
  • ax.set_xlabel: To add a x-axis text label. We set the font size to 15.
  • ax.set_ylabel: To add a y-axis text label. We set the font size to 10.
matplotlib title different font size
“Different Title Font Size”

You may like the following Python Matplotlib tutorials:

In this Python tutorial, we have discussed the “Matplotlib title font size” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.

  • Matplotlib title font size
  • Matplotlib set title font size
  • Matplotlib title font size bold
  • Matplotlib legend title font size
  • Matplotlib subplot title font size
  • Matplotlib rcParams title font size
  • Matplotlib pie chart title font size
  • Matplotlib bar chart title font size
  • Matplotlib colorbar title font size
  • Matplotlib figure title font size
  • Matplotlib set_title text font size
  • Matplotlib title different font size