How to add a title on Seaborn lmplot?

To add a title on Seaborn lmplot(), we can use the set_title() method on the plot axes. The lmplot() function creates a scatter plot with a linear regression line, and we can customize it with a descriptive title.

Steps to Add Title

  • Create a Pandas DataFrame with sample data
  • Use lmplot() method to create the regression plot
  • Get the current axis using gca() method
  • Add title using set_title() method
  • Display the plot using show() method

Example

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
df = pd.DataFrame({
    "X-Axis": [np.random.randint(10) for i in range(10)], 
    "Y-Axis": [i for i in range(10)]
})

# Create lmplot
bar_plot = sns.lmplot(x='X-Axis', y='Y-Axis', data=df, height=3.5)

# Get current axis and add title
ax = plt.gca()
ax.set_title("Random Data Linear Model Plot")

plt.show()

Alternative Method Using FacetGrid

Since lmplot() returns a FacetGrid object, you can also add a title using the fig.suptitle() method ?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Create sample data
df = pd.DataFrame({
    "Sales": [20, 35, 30, 35, 27, 25, 40, 45, 38, 42], 
    "Temperature": [15, 25, 20, 28, 22, 18, 30, 32, 28, 35]
})

# Create lmplot and add title using suptitle
plot = sns.lmplot(x='Temperature', y='Sales', data=df, height=4)
plot.fig.suptitle('Sales vs Temperature Relationship', y=1.02)

plt.show()

Customizing Title Appearance

You can customize the title's font size, weight, and position ?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Create sample data
df = pd.DataFrame({
    "Hours_Studied": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    "Test_Score": [55, 60, 65, 70, 75, 80, 85, 88, 92, 95]
})

# Create lmplot with custom title
plot = sns.lmplot(x='Hours_Studied', y='Test_Score', data=df, height=4)
plot.fig.suptitle('Study Hours vs Test Performance', 
                  fontsize=16, 
                  fontweight='bold', 
                  y=1.05)

plt.show()

Comparison of Methods

Method Syntax Best For
ax.set_title() plt.gca().set_title("Title") Simple single plots
fig.suptitle() plot.fig.suptitle("Title") Better positioning control

Conclusion

Use plt.gca().set_title() for quick titles or plot.fig.suptitle() for better control over positioning. The suptitle() method is preferred as it provides more customization options for font size, weight, and position.

Updated on: 2026-03-25T22:14:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements