Adding a title to a Seaborn line plot (lmplot)
This tutorial will teach you how to add a title to a lineplot (lmplot) in the Seaborn library. To perform this, we require two libraries – Seaborn and matplotlib.pyplot
.
Pyplot is nothing but a module in matplotlib used to easily plot graphs in a convenient way.
Importing the required libraries
Seaborn and matplotlib libraries are imported using the following code:-
import seaborn as sns import matplotlib.pyplot as plt
Both libraries are imported using aliases with the keyword ‘as’. This helps write clean and good-looking code.
Plotting a line plot
Before learning how to add a title to a line plot, I would suggest you learn how to plot a line plot using the following link:-
Line chart plotting using Seaborn in Python
Giving a title to a line plot in Seaborn
We may use the plt.title()
function to give a title to a line plot. The code below demonstrates this:-
#Importing the necessary libraries import seaborn as sns import matplotlib.pyplot as plt #Loading the dataet dataset = sns.load_dataset("planets") #Plotting the line plot with x-axis as distance and y-axis as year sns.lmplot(x="distance",y="year",data=dataset ) #Taking an input to the title of the plot plot_title = input("Enter The Title To Your Plot : ") #Adding the title to the plot plt.title(plot_title) #Showing the plot plt.show()
First, we import the libraries, seaborn and matplotlib.pyplot
.
Next, the function load_dataset()
loads the dataset, ‘planets’ into the variable, ‘dataset’.
Subsequently, the function lmplot()
plots the line plot with the data from the dataset.
The input()
function then asks for a title for the plot. The title()
function then sets the title.
Finally, the show()
function shows the plot.
Output
I have set the title to ‘test’ for the sake of this example.
In conclusion, I encourage you to explore more about seaborn and graphs in Python. For this purpose, you may refer to the links below:-
Leave a Reply